The simple Formatter Function example code _javascript technique in JS

Source: Internet
Author: User

JS native does not provide a convenient use of the formatter function, the use of character stitching method looks confusing and difficult to read, and it is inconvenient to use. Personally feel the syntax provided in C # is more useful, such as:

String.Format ("Welcome to learn ' {0} ', ' {0} ' is awesome,you'll {1} it!", "Javascript", "Love");

This is a sequential replacement, more clear, and when you want to replace the same content can be omitted to pass the repeated parameters, the following is a simple implementation of JS version (no rigorous testing):

(function (Exports) {
Exports.format = function () {
var args = Array.prototype.slice.call (arguments),
Sourcestr = Args.shift ();

function Execreplace (text,replacement,index) {return
text.replace (new RegExp ("\\{" +index+ "\}", ' G '), replacement);
}

Return Args.reduce (EXECREPLACE,SOURCESTR);
}
) (window.utils = Window.utils | | {});

Console.log (Utils.format ("Welcome to learn ' {0} ', ' {0} ' is awesome,you'll {1} it!", "Javascript", "Love"));

The key is the sentence:

Args.reduce (EXECREPLACE,SOURCESTR);

The reduce function of the array is used here, and reduce and reduceright are the newly added functions of ES5, the parameter of the function is reduce (callback,initialvalue), and the callback receives 4 parameters, respectively:

Previousvalue:

When traversing the callback function for the first time, if Initivalvalue is specified, the initivalvalue is used directly if the first element that will use the array is not specified
Second and subsequent traversal the value is the result of the previous traversal return
The result returned by the last traversal will be the return value of the reduce function
CurrentValue: The current item traversed to
Index: Subscript of the current item in an array

Array: Original array

Use the result of the previous substitution as the original replacement string for each execution in Execreplace, using the index of the current item as the content to be replaced, traversing sequentially, and finally completing the replacement.

Note: Reduceright is basically the same as the reduce function, except that its traversal order is from right to left

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.