difference between numbers using or without using variable

后端 未结 3 1663
盖世英雄少女心
盖世英雄少女心 2020-12-21 01:47

What\'s the difference between the following code?

var a = 1;
a.toString(); // outputs: \"1\"

But this throws an error:

1.t         


        
3条回答
  •  -上瘾入骨i
    2020-12-21 02:06

    The toString() method returns a string representing object.

    So when you call:

    a.toString();
    

    You are actually operating on an object. You are actually creating a built-in object when you define a variable(in this case it is a number).

    When you do this:

    1.toString();
    

    toString() doesn't see 1 as an object or a variable(both are the same in this scenario) because it fails the rule:

    Variable must begin with a letter

    Here 1 doesn't begin with a letter. So toString() knows it is not operating on an object and throws an error.

提交回复
热议问题