100.toString vs 100 ['toString']

100['toString'] //does not fail
100.toString //fails

why?

100.toString is not the same as 100.toString (). So why in the second case I do not get the function as a return value?

+5
source share
3 answers

The second line fails because it is parsed as the number "100" and then "toString".

To use 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 a function toString, you will also need to include 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), because the alternatives can be confusing and unintuitive.

+17
source

(100).toString .

+5

Parens - . , .

function () {}.call() => fails
(function () {}).call() => succeeds
+1

All Articles