Simple Formatter function sample code and formatter sample code are implemented in JS.

Source: Internet
Author: User

Simple Formatter function sample code and formatter sample code are implemented in JS.

JS native does not provide easy-to-use Formatter functions. Character concatenation seems confusing and difficult to read, and it is inconvenient to use. I personally feel that the syntax provided in C # is relatively easy to use, such:

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

This ordered replacement method is clear and saves the trouble of passing repeated parameters when you want to replace the same content. Below is a simple JS implementation version (no strict test ):

(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 will {1} it!","Javascript","love"));

The key is:

args.reduce(execReplace,sourceStr);

The reduce function of Array is used here. reduce and reduceRight are newly added functions of es5. the parameters of this function are reduce (callback, initialValue), and callback receives the following four parameters:

Previusvalue:

If initivalValue is specified, initivalValue is directly used when traversing the first entry into the callback function. If initivalValue is not specified, the first element of the array is used.
The second and later traversal. This value is the result returned by the previous traversal.
The result returned by the last traversal is used as the return value of the reduce function.
CurrentValue: the current item to be traversed.
Index: subscript of the current item in the array

Array: original array

During each execution of execReplace, the result after the previous replacement is used as the original replacement string, the index of the current item is used as the content to be replaced, and the replaced content is traversed in sequence.

Note: The reduceRight and reduce functions are basically the same, but their traversal order is from right to left.


How to Use JS to download files? functions and code

In this method, as long as the page Jump is used to direct the url to the file address, IE will download it.
For example, download xx.doc under the Upload folder.
Function download ()
{
Location. href = "../Upload/XX.doc ";
}

In js, how does one change the added image to an event where you can click an image to call functions in js? How does one compile code?

This. parentNode can get its own container, and find the container of the entire line based on your code structure, for example, if a table is used, the tr table is used. For example, <table id = "list"> <tr> <td> <a href = "javascript: void (0 ); "onclick =" delRow (this. parentNode. parentNode); "> </td> </tr> </table>
<Script> function delRow (obj ){
Var tb = document. getElementById ('LIST ');
Tb. rows. removeChild (obj );
}
</Script>

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.