最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 100.toString vs 100['toString'] - Stack Overflow

programmeradmin3浏览0评论
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 0
Add a ment  | 

3 Answers 3

Reset to default 18

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)

  1. 暂无评论