100['toString'] //does not fail
100.toString //fails
why?
100.toString is not same as 100.toString() . So why in the second case I am not getting the function as returned value?
100['toString'] //does not fail
100.toString //fails
why?
100.toString is not same as 100.toString() . So why in the second case I am not getting the function as returned value?
Share Improve this question asked Mar 11, 2010 at 22:09 Nick VanderbiltNick Vanderbilt 38.5k30 gold badges85 silver badges108 bronze badges 03 Answers
Reset to default 18The second line fails because it is parsed as a number "100.", followed by "toString".
To use the dot notation, Any of the following will work:
(100).toString
100.0.toString
100..toString
var a = 100;
a.toString
If you are trying to call the toString
function , you will also need to include the parentheses:
(100).toString()
100.0.toString()
100..toString()
var a = 100;
a.toString()
I prefer using parentheses (or a variable, if I already have one obviously), because the alternatives could be confusing and unintuitive.
Use (100).toString
instead.
Parens is the best way to go. You've got the same issue w/ function definitions as well.
function () {}.call() => fails
(function () {}).call() => succeeds