In Ruby, the "*" operator used with a string on the left and a number on the right does string repetition. "Ruby"*2 evaluates to "RubyRuby", for example. This is only occasionally useful (when creating lines of hyphens for ASCII tables, for example) but it seems kind of neat. And it sure beats having to write a loop and concatenate n copies of a string one at a time--that just seems really inefficient.
I just realized that there is a clever way to implement string multiplication in JavaScript:
String.prototype.times = function(n) {
return Array.prototype.join.call({length:n+1}, this);
};
"js".times(5) // => "jsjsjsjsjs"
This method takes advantage of the behavior of the Array.join() method for arrays that have undefined elements. But it doesn't even bother creating an array with n+1 undefined elements. It fakes it out using and object with a length property and relies on the fact that Array.prototype.join() is defined generically. Because this object isn't an array, we can't invoke join() directly, but have to go through the prototype and use call(). Here's a simpler version that might be just as efficient:
String.prototype.times = function(n) { return (new Array(n+1)).join(this);};
When you call the Array() constructor with a single numeric argument, it just sets the length of the returned array, and doesn't actually create any elements for the array.
I've only tested these in Firefox. I'm assuming that either is more efficient than anything that involves an actual loop, but I haven't run any benchmarks.
解釋
我的英語非常爛,沒辦法為大家逐字逐句地翻譯,只能為大家解釋一下大概的意思。
在Ruby中,“*”操作符用一個字符串作為左邊參數,一個數字作為右邊參數,來實現字符串重復。例如,"Ruby" * 2 的值為 "RubyRuby"。這僅在少數地方有用(例如,生成一張由連字符等ASCII 碼字符構成的表格),但是非常簡潔。而且好過寫一個循環來連接n次字符串——這樣顯得很沒效率。
我剛剛發現在JavaScript中有個聰明的技巧來實現字符串的乘法:
代碼請參見原文
這個方法是調用一個由元素全為“undefined”的數組的Array.join()行為。但是它并沒有真正創建一個包含 n+1 個“undefined”元素的數組。它利用一個包含 length 屬性的匿名對象,依靠 Array 對象的原型函數 join()。因為 “Object” 不是數組,不能直接調用 join(),因此不得不通過原型的 call() 來實現。下面給出一個同樣效果的簡單版本:
代碼請參見原文
當我們調用 Array 的帶一個參數的構造器時,僅僅是設置了數組的長度,實際上并沒有創建數組的元素。
我僅在 Firefox 下對其做了測試,我估計它會比普通的循環更加有效,但我并沒有進行基準測試。
作者簡介
David Flanagan 是一個醉心于Java寫作的計算機程序員,他的大部分時間都致力于編寫Java相關圖書。David 在麻省理工學院獲得了計算機科學于工程學位。他生活在地處西雅圖和溫哥華之間的美國太平洋西北海岸。他在O'Reilly出版的暢銷書有《Java in a Nutshell》、《Java Foundation Classes in a Nutshell》、《Java Enterprise in a Nutshell》、《JavaScript: The Definitive Guide》、《JavaScript Pocket Reference》以及《The Ruby Programming Language》等。
我的評論
如果要考慮效率的話,對循環迭代稍作優化可能效率更高。比如下面這段遞歸調用,算法復雜度是O(log2n)。在Google Chrome下測試結果是比 David 的方法執行更快,但不得不承認他的方法很優雅!
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;
}
后記
David 采納了我的建議,他又為我們寫了一段非遞歸的版本。請參看他的博客原文:http://www.davidflanagan.com/2009/08/good-algorithms.html
出處:http://blog.csdn.net/redraiment/