在 Ruby 中,您可以这样做:
3.times { print "Ho! " } # => Ho! Ho! Ho!
我尝试用 JavaScript 来做:
Number.prototype.times = function(fn) {
for (var i = 0; i < this; i++) {
fn();
}
}
这有效:
(3).times(function() { console.log("hi"); });
这不
3.times(function() { console.log("hi"); });
Chrome 给我一个语法错误:“意外的标记非法”。为什么?
The .
在数字代表数字的小数点之后,您必须使用另一个数字来访问属性或方法。
3..times(function() { console.log("hi"); });
这仅对于十进制文字是必需的。对于八进制和十六进制文字,您只能使用一种.
.
03.times(function() { console.log("hi"); });//octal
0x3.times(function() { console.log("hi"); });//hexadecimal
也是指数级的
3e0.times(function() { console.log("hi"); });
您还可以使用空格,因为数字中的空格是无效的,这样就不会产生歧义。
3 .times(function() { console.log("hi"); });
虽然如所述wxactly
在注释中,缩小器会删除导致上述语法错误的空格。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)