JavaScript中的字符串乘法

原文

原文地址:http://www.davidflanagan.com/2009/08/string-multipli.html

原作者:David Flanagan

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:

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:

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 的方法执行更快,但不得不承认他的方法很优雅!

后记

David 采纳了我的建议,他又为我们写了一段非递归的版本。请参看他的博客原文:http://www.davidflanagan.com/2009/08/good-algorithms.html


版权声明

本人的所有原创文章皆保留版权,请尊重原创作品。
转载必须包含本声明,保持本文完整,并以超链接形式注明原始作者“redraiment”和主站点上的本文原始地址。

联系方式

我的邮箱,欢迎来信(redraiment@gmail.com
我的Blogger(子清行
我的Google Sites(子清行
我的CSDN博客(梦婷轩
我的百度空间(梦婷轩

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值