Is there a difference between String(x) and ''

后端 未结 4 473
陌清茗
陌清茗 2021-01-22 20:44

Is there a difference? Will string 2 inherit different object prototypes?

var s1 = 1234 + \'\';
var s2 = String(1234);

//s1.someNewFunc();   error?
//s2.someNew         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-22 21:03

    Both will behave the same way.

    Also, there is a nice explanation about string primitives vs. objects here:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

    Distinction between string primitives and String objects

    [...] String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings. [...]

提交回复
热议问题