开发者

100.toString vs 100['toString']

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 no开发者_如何学JAVAt getting the function as returned value?


The 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜