Home Web Front-end JS Tutorial How to extend string concatenation in JS

How to extend string concatenation in JS

Apr 11, 2018 pm 02:19 PM
javascript string Expand

这次给大家带来JS怎样扩展字符串拼接,JS扩展字符串拼接的注意事项有哪些,下面就是实战案例,一起来看一下。

1、题外话,有关概念理解:String.prototype 属性表示 String原型对象。所有 String 的实例都继承自 String.prototype. 任何String.prototype上的改变都会影响到所有的 String 实例。

2、上正文,js扩展String.prototype.format字符串拼接的功能,首先是基础功能的改造:

String.prototype.format = function(){
  if(arguments.length==0){
    return this;
  }
  for(var s=this, i=0; i<arguments.length; i++){
    s = s.replace(new RegExp("\\{"+i+"\\}","g"), arguments[i]);
  }
  return s;
};
Copy after login

3、然后就是调用方式:

//方式1
var test = '我的{0}是{1}';
var result = test.format('id','城市之光');
//方式2
var test = '我的{name1}是{name2}';
var result = test.format({name1:'id',name2:'城市之光'});
Copy after login

4、就这么简单,附带一个对trim()的扩展

String.prototype.trim = function() { 
 return this.replace(/(^\s*)|(\s*$)/g, ""); 
};
String.prototype.ltrim = function() { 
 return this.replace(/(^\s*)/g, ""); 
};
String.prototype.rtrim = function() { 
 return this.replace(/(\s*$)/g, ""); 
};
//调用方式
var eg1 = $('#id').val().trim();
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

为什么element-ui绑定@keyup事件无效?

在angular4.0路由传递获取参数的最优方案

The above is the detailed content of How to extend string concatenation in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Extensions and third-party modules for PHP functions Extensions and third-party modules for PHP functions Apr 13, 2024 pm 02:12 PM

Extensions and third-party modules for PHP functions

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

How to determine whether a Golang string ends with a specified character

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang?

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

How to repeat a string in python_python repeating string tutorial

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP string manipulation: a practical way to effectively remove spaces

Learn more about how to use the Laravel Redis extension Learn more about how to use the Laravel Redis extension Mar 09, 2024 pm 02:03 PM

Learn more about how to use the Laravel Redis extension

See all articles