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

后端 未结 4 419
陌清茗
陌清茗 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:01

    var s1 = 1234 + '';
    

    Creates a string literal. This is a javascript language primitive.

    var s2 = String(1234);
    

    The String() function also returns a primitive string literal. s2 will have the same members as s1 because they are both the same type.

    However

    var s3 = new String("1234");
    

    Will create an object of type String rather than a primitive string literal. This does have different members and is of type object.

提交回复
热议问题