JavaScript string Multiplication _javascript tips

Source: Internet
Author: User

Original address:http://www.davidflanagan.com/2009/08/string-multipli.html
Original Author: David Flanagan
In Ruby, the "*" operator uses a string as the left argument and a number as the right argument to duplicate the string. For example, the value of "Ruby" * 2 is "Rubyruby". This is only useful in a few places (for example, to generate a table composed of ASCII characters such as hyphens), but it is very concise. It's better than writing a loop to connect n times a string--it's inefficient.

I've just discovered that there's a clever trick in JavaScript to implement string multiplication:

Copy Code code as follows:

String.prototype.times = function (n) {
Return Array.prototype.join.call ({length:n+1}, this);
};
"JS". Times (5)//=> "Jsjsjsjsjs"


This method calls a array.join () behavior of an array of elements that are all "undefined". But it doesn't really create an array that contains n+1 "undefined" elements. It leverages an anonymous object that contains the length property and relies on the prototype function join () of the Array object. Because "Object" is not an array, the join () cannot be called directly, and therefore has to be implemented through the prototype call (). A simple version of the same effect is given below:

Copy Code code as follows:

String.prototype.times = function (n) {return (new Array (n+1)). Join (this);

When we call the array's constructor with one parameter, we simply set the length of the array and do not actually create the elements of the array.

I just tested it under Firefox and I figured it would be more efficient than a normal loop, but I didn't do the benchmark.

Author Introduction
David Flanagan, a computer programmer obsessed with Java writing, spends most of his time writing Java-related books. David obtained a degree in engineering from the Massachusetts Institute of Science. He lives on the Pacific Northwest coast of the United States, located between Seattle and Vancouver. His best-selling book, O ' Reilly, is "Java in a nutshell", "Java Foundation Classes in a nutshell", "Java Enterprise in a nutshell", "JavaScript: The definitive Guide, "JavaScript Pocket Reference" and "the Ruby programming Language".

My comments
If efficiency is to be considered, it may be more efficient to slightly optimize the loop iterations. For example, the following recursive call, the algorithm complexity is O (log2n). The test results are faster than David's method in Google Chrome, but they have to admit that his approach is elegant!
Copy Code code as follows:

String.prototype.times = function (n) {
if (n = = 1) {
return this;
}
var midres = This.times (Math.floor (N/2));
Midres + = Midres;
if (n% 2) {
Midres + = this;
}
return midres;
}

Postscript
David took my advice and he wrote us a non recursive version. Please refer to his blog in the original: http://www.davidflanagan.com/2009/08/good-algorithms.html
Contact Way
My email, welcome letter (Redraiment@gmail.com)

Related Article

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.