What is the difference between “new Number(…)” and “Number(…)” in JavaScript?

后端 未结 4 718
青春惊慌失措
青春惊慌失措 2020-12-02 01:48

In Javascript, one of the reliable ways to convert a string to a number is the Number constructor:

var x = Number(\'09\'); // 9, because it defa         


        
4条回答
  •  半阙折子戏
    2020-12-02 02:27

    Number (without new) doesn't seem to result exactly in a primitive. In the following example the anyMethod() is called (if in the Number prototype).

    Number(3).anyMethod()
    

    Whereas

    3.anyMethod()
    

    will not work.

提交回复
热议问题