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

后端 未结 4 421
陌清茗
陌清茗 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 20:52

    Javascript allows you to treat primitive values as though they were objects. It does so by doing an on-the-fly coercion of the primitive to an object. That's why, even though primitives have no properties, something like this is perfectly fine:

    "abcde".substr(1,3); //bcd
    true.valueOf(); //true
    

提交回复
热议问题