String. format function supplement _ javascript skills in javascript

Source: Internet
Author: User
In the previous article, Dong Qian, a lazy, commented that the format function rewritten by Lou pig did not work after 11 input parameters, and then re-read Andrew's original article, in the comments below the original text, it was suddenly found that some people raised the question about the number of parameters. Similarly, the lazy Lou pig directly copied the original text and replied, at the same time, it is also found that a very important note is missing. Array. prototype. slice.
The following provides a unified explanation:

1. Number of string. format parameters
In Andrew's original article, someone has pointed out:

Eric d. Hi, thanks for that brilliant article. Made a lot of things a lot clearer!
Note: new RegExp ("% ([1-" + arguments. length + "])", "g"); will fail passed 9 arguments (the regexp wocould be "% ([1-10]) "so it will only match % 0 and % 1 ).

I think an easy fix wocould be 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 was only an example and brevety is the main objective, but its a great function to have)

Posted on: January 20th 2009, am

The guy who left the message gave the author enough face, saying "I understand it was only an example and brevety is the main objective, but its a great function to have ". It turns out that the range of numbers that can be verified by the regular expression defined in the original article is... it turns out that, haha, Lou pig smiled.

2. Array. prototype. slice
The method to convert arguments to Array is through Array. prototype. slice. call (arguments); Method conversion, that is to say, all objects in the class Array mode can be converted to the Array through slice. When it comes to the conversion of class Array objects, it is really necessary to record and summarize the Array Prototype Method slice.
(1) common usage
Lou pig introduced the slice method through a code section earlier: slice (start, end): returns a subset of the array object, the original array is not affected when the index starts from start (including start) to end (excluding end. In fact, we can boldly guess that the slice function should define an Array variable, then push the index value of the Array into the variable through a loop, and finally return the Array variable.
(2) "not an Array, we also want to become an Array"
It is not an Array, but it has the length attribute. You can convert it to a real Array Based on the index value, for example, arguments in this article:

The Code is 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 can see that the push and pop methods work. Similarly, Nodelist has similar features. How to convert NodeList to Array? Readers who have read Lou Piao's original article may think this is a cliché. Let's say, in IE, Array. prototype. slice. call (nodelist) is not the case. Finally, paste the method to convert NodeList to Array and be compatible with ie and other browsers:

The Code is 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

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.