JS expands the String. prototype. format String concatenation function,
1. concepts: the String. prototype attribute indicates the String prototype object. All String instances inherit from String. prototype. Any changes to String. prototype will affect all String instances.
2. In the above text, js expands the String. prototype. format String concatenation function. The first is the transformation of basic functions:
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;};
3. The call method is as follows:
// Method 1var test = 'my {0} is {1} '; var result = test. format ('id', 'city light'); // method 2var test = 'my {name1} is {name2} '; var result = test. format ({name1: 'id', name2: 'city light '});
4. This is simple, with an extension of 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, "") ;}; // call method var eg1 =1 ('# id '). val (). trim ();
Summary
The above is the JS extension String introduced by xiaobian. prototype. the format String concatenation function is helpful to you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!