日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

JavaScript中的字符串乘法

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

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/

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 久久免费国产精品 | 亚洲精品v天堂中文字幕 | 精品视频在线免费观看 | 亚洲第一黄色 | 懂色av影视一区二区三区 | 久久久精品一区二区 | 亚洲综合另类 | 欧美不卡 | 亚洲视频精品 | 欧美日韩免费网站 | 久久久久久中文字幕 | 日韩影片在线观看 | 神马久久精品 | 最新日韩在线观看视频 | 国产精品页 | 国产无套精品一区二区 | 国产美女精品 | 久久精品91久久久久久再现 | 免费一区二区 | 玖玖久久| 91网国产在线观看 | 亚洲精品国产精品乱码 | 精品国产髙清在线看国产毛片 | 五月天丁香综合久久国产 | 成人妇女免费播放久久久 | 亚欧洲精品视频在线观看 | 一级毛片成人免费看a | 91射区| 中文字幕亚洲一区二区三区 | 欧美日韩精品在线观看 | 精品欧美一区二区三区 | 国产成人视屏 | 国产在线精品成人免费怡红院 | 精品天堂 | 天堂一区 | 久久久www | 亚洲综合久久久久 | 99九九热 | 国产美女福利 | 欧美中文在线观看 | 一区二区三区高清不卡 |