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

Using arguments to implement string.format function under javascript

高洛峰
Release: 2016-12-14 10:03:05
Original
1101 people have browsed it

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

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]; 
}); 
};
Copy after login

Through the format function, the following code:

The code is as follows:

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

It 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#.
In general, it seems that there is really no technical content. But is there really no technical content? Lou Zhu boldly analyzes this program based on his superficial knowledge and understanding of javascript and arguments:
1. Regular expression
Very cleverly created a new regular pattern starting with % that matches the number of arguments from 1 to 1. This Regularity 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, which 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:

Code 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("参数索引出错"); 
} 
return args[currentIndex + 1]; 
}); 
}; 
document.write(format("And the {0} want to know whose {1} you {2}", "papers", "shirt", "wear"));//大括号,索引从0开始...
Copy after login

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 it could be used in custom events I used it to define the createFunction function, and used the createFunction function to construct a parameterless function for use in events, but at that time I was always depressed that "I only knew 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.

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!