Implement the string. Format function 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. He 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 passed a section in the previous articleCodeSlice (START, end): returns a subset of the array object. The index starts from start (including START) to end (excluding end ), the original array is not affected. 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:

 
Function Test () {var ARGs = array. prototype. slice. call (arguments); alert (ARGs. length); args. push ("Jeff"); // pushargs. push ("Wong"); alert (ARGs. length); // 2 alert (ARGs. pop (); // popalert (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:

 
VaR nodelist = something; // A nodelist variable VAR arr = NULL; // array try {// iearr = new array (); For (VAR I = 0; I <nodelist. length; I ++) {arr. push (nodelist [I]);} catch (e) {// other browsers arr = array. prototype. slice. call (nodelist );}

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.