多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > web前端 > jscript > JavaScript中的字符串乘法

JavaScript中的字符串乘法

來源:程序員人生   發布時間:2014-04-19 09:30:36 閱讀次數:3355次

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/

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产亚洲一区在线 | 波多野结衣在线一区 | 啪啪欧美| 性xxxxfreexxxxx国产 | 欧美日韩国产不卡在线观看 | 日本一区二区三区在线网 | 亚洲 欧美 激情 另类 校园 | 午夜亚洲国产理论秋霞 | 精品三级视频 | asianjapanese日本护士 | 欧美一区二区三区在线播放 | 国产精品一区二区三区四区 | 黑粗硬大欧美视频 | 国产精品久久久久影视不卡 | 免费视频网站在线看视频 | 久久91亚洲精品久久91综合 | 免费爱爱片 | 自拍偷拍二区 | 国产在线观看成人 | 欧美一级毛片无遮 | 久久久久综合国产 | 日韩 欧美 亚洲国产 | 亚洲国产精品a一区二区三区 | 国产精品久久久久国产精品三级 | 狂野黑人性猛交xxxxxx | 国产dvd毛片在线视频 | 日本一视频一区视频二区 | 欧美另类 videos黑人极品 | 日本午夜视频在线观看 | 欧美亚洲国产精品 | 精品视频在线播放 | 视频二区好吊色永久视频 | 男人和女人全黄一级毛片 | 中国国产一国产一级毛片视频 | 黄色网址中文字幕 | 欧美freesex10一13 | 69视频在线| tube日本黑人杂交 | 国产亚洲精品久久久久久久久激情 | 又大又硬又黄又刺激的免费视频 | 亚洲三级黄色片 |