javascript 字符串的乘法
在ruby中我们可以通过"*"操作符去字符串进行倍增,如"ruby"*2则返回"rubyruby"。在javascript中,字符串只能用加号,嘛,乘法也加法演变过来的。我们可以搞一个试试。
方法一
String.prototype.times = function(n) {//IE6 530-640 FF3 400~550 IE8 840 ~1110 chrome 600~1000
return (new Array(n+1)).join(this);
};
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<title>字符串的乘法</title>
<script type="text/javascript" charset="utf-8">
window.onload = function(){
String.prototype.times = function(n) {
return (new Array(n+1)).join(this);
};
var s = "司徒正美"
var start = new Date();
a = s.times(1000000);
var end = new Date();
alert("所耗时间 " + (end-start));
}
</script>
</head>
<body>
<pre>
String.prototype.times = function(n) {
return (new Array(n+1)).join(this);
};
var s = "司徒正美"
var start = new Date();
a = s.times(1000000);
var end = new Date();
alert("所耗时间 " + (end-start));
</pre>
</body>
</html>
运行代码
创建一个n+1的空数组,调用join方法。
方法二
String.prototype.times = function(n) {//IE6 570~600 FF3 320~430 chrome 550~900 IE8 422~490
return Array.prototype.join.call({length:n+1}, this);
};
<!doctype htm
相关文档:
jQuery片段:
var
// Will speed up references to window, and allows munging its name.
window = this,
// Will speed up references to undefined, and allows munging its name.
undefined,
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrit ......
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table
2. <body onselectstart="return false"> 取消选取、防止复制
3. onpaste="return false" 不准粘贴
4. oncopy="return false;" oncut=" ......
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table
2. <body onselectstart="return false"> 取消选取、防止复制
3. onpaste="return false" 不准粘贴
4. oncopy="return false;" oncut="re ......