The technique of adding _javascript to String.Format function in JavaScript

Source: Internet
Author: User
Read again Andrew's original text, in the comments below the original text, impressively found that some people have raised the question of the number of parameters, the same lazy pig direct copy of the original comment reply, but also found that the leakage of a very important attention point Array.prototype.slice.
The following unified add to the explanation:

1, string.format number of parameters
In Andrew's original text, it has been pointed out that:

Eric D. Hi, thanks to that brilliant article. Made a lot of things a lot clearer!
Note:new RegExp ("% ([1-" + Arguments.length + "])", "G"); Would fail passed 9 arguments (the regexp would is "% ([1-10])" So it'll only match%0 and% 1).

I am easy fix would to something like:
function Format (string) {var args = arguments var pattern = new RegExp ("% ([0-9]+)", "G"); return string (String). replace (pattern, function (match, index) {if (index = = 0 | | Index >= args.length) throw "Invalid Index in format string "; return Args[index]; }); };
(Sorry for nitpicking, I understand it's only a example and brevety are the main objective, but its a great function to Have

Posted On:january 20th 2009, 12:01 am

The guy who wrote the message gave the author face, saying that "I understand it was the" example "and brevety is the main objective, but its a great function to have". Originally, the regular expression defined in the original text can verify the range of numbers ... So it is, Haha, Lou pig false smile.

2, Array.prototype.slice
The way to convert arguments to array is through Array.prototype.slice.call (arguments), which means that objects of the class array can be converted to array in slice way, When it comes to the conversion of class array objects, it is really necessary to slice the prototype method of the array.
(1), common usage
In this earlier article, Lou Pig passed through a piece of code. Slice method: Slice (Start,end): Returns a subset of the array objects, starting with start (including start), ending with end (excluding ends), and the original array unaffected. In fact, we can make a bold guess the slice function should be defined as an array variable, and then through the loop, the array corresponding to the index value push into the variable, and finally return the array variable.
(2), "Not array, we also want to become array"
is not an array, but has a length attribute, which can be evaluated by index, such as the arguments in this article, which we can convert to a real array in the following way:

Copy Code code as follows:

function Test () {
var args = Array.prototype.slice.call (arguments);
alert (args.length);
Args.push ("Jeff"); Push
Args.push ("Wong");
alert (args.length); 2
Alert (Args.pop ()); Pop
alert (args.length); 1
}
Test ();

We see both the push and pop methods working. Similarly, nodelist has similar characteristics. How do I convert nodelist to array? The reader who has seen the story of the building pig may think that this is a cliché, or to say more, in IE, Array.prototype.slice.call (nodelist) is not so, Finally, it's time to paste the NodeList into array and compatible with IE and other browsers to end this article:
Copy Code code as follows:

var nodelist =something;//a nodelist variable
var arr = null; Array
try {//ie
arr = new Array ();
for (var i = 0; i < nodelist.length; i++) {
Arr.push (Nodelist[i]);
}
catch (E) {//other browsers
arr = Array.prototype.slice.call (nodelist);
}

Author: Jeff Wong
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.