Home > Web Front-end > JS Tutorial > body text

Using arguments to implement string.format function under javascript_javascript skills

WBOY
Release: 2016-05-16 18:20:57
Original
846 people have browsed it

Below is an excerpt of the source code and an in-depth analysis of his design and implementation ideas:

Copy the code The code is as follows:

function format(string) {
var args = arguments;
var pattern = new RegExp("%([1-" arguments.length "])", "g");
return String (string).replace(pattern, function(match, index) {
return args[index];
});
};

Through the format function, the following Code:
Copy code The code is as follows:

format("And the %1 want to know whose %2 you %3", "papers", "shirt", "wear");

will return: "And the papers want to know whose shirt you wear" .
Sure enough, it is a bit like passing parameters and calling the string.format function in C#.
Generally speaking, it seems that there is really no technical content. But is there really no technical content? Lou Zhu dares to analyze this program based on his superficial knowledge and understanding of javascript and arguments:
1. Regular expression
very cleverly creates a new one starting with % that matches the number of arguments from 1 to 1. Regular pattern, this regular pattern is an important prerequisite for string replacement in point 2 below;
2. Replace function of string
The second parameter of the replace function is function, which is very "surprising". By definition, the variable args is actually arguments, and then the index parameter can be obtained through args[index], and index>=1 and index
The function is so short and concise, and it forms a huge contrast with the powerful functions, which is amazing.
There may be many developers who are spoiled by C# like Lou Zhu, and will be obsessed with the writing method of string.format in C# (most of them are caused by usage habits, right?). Good thing Lou Zhu has slightly changed the source code:
Copy code The code is as follows:

function format(string) {
var args = arguments;
var pattern = new RegExp("{([0-" arguments.length "])}", "g");
return String(string).replace(pattern, function(match, index) {
var currentIndex = parseInt(index);
if (currentIndex 1 > args.length || currentIndex < 0) {
throw new Error("Parameter index error");
}
return args[currentIndex 1];
});
};
document.write(format("And the {0} want to know whose {1} you {2}", "papers ", "shirt", "wear"));//Braces, the index starts from 0...

This way it looks like the format function can be called like the c# writing style.
The last time I checked the writing of this article was in 2008. Lou Zhu was quite enlightened in 2008. He was working hard to learn JavaScript, but his understanding of arguments was still very immature. Although he already knew that he could use it in his own language. It is used to define the createFunction function when defining an event, and the createFunction function is used to construct a parameterless function for use in the event, but at that time I was always depressed that "I only know its shape but not its reality." After reading Andrew's masterpiece, I suddenly felt enlightened. Although my reaction was slow and I realized it later, I still felt extremely inspired and gratified.

Please read Andrew Tetlaw’s original text. In fact, someone below has pointed out that the function will not work after the format function parameter exceeds 9, and then also gave a solution:

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 would be "%([1 -10])" so it will only match %0 and %1).

I think an easy fix would 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, 12:01 am

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template