加入收藏 | 设为首页 | 会员中心 | 我要投稿 河北网 (https://www.hebeiwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

JavaScript中的字符串乘法

发布时间:2018-09-12 06:40:19 所属栏目:创业 来源:站长网
导读: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 ex

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/

(编辑:河北网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读