Why Doesn't Javascript Let You Call Methods On Numbers Directly?
In Ruby, you can do this: 3.times { print 'Ho! ' } # => Ho! Ho! Ho! I tried to do it in JavaScript: Number.prototype.times = function(fn) { for (var i = 0; i < this; i++
Solution 1:
The .
after the digits represents the decimal point of the number, you'll have to use another one to access a property or method.
3..times(function() { console.log("hi"); });
This is only necessary for decimal literals. For octal and hexadecimal literals you'd use only one .
.
03.times(function() { console.log("hi"); });//octal0x3.times(function() { console.log("hi"); });//hexadecimal
Also exponential
3e0.times(function() { console.log("hi"); });
You can also use a space since a space in a number is invalid and then there is no ambiguity.
3 .times(function() { console.log("hi"); });
Although as stated by wxactly
in the comments a minifier would remove the space causing the above syntax error.
Post a Comment for "Why Doesn't Javascript Let You Call Methods On Numbers Directly?"