var s0 = 'hello';
var s1 = new String(s0);
var s2 = String(s0);

一个是用构造函数返回字符串本身——基本类型,一个是实例化成对象可以自定义属性和方法——特殊对象类型

console.log(s1 === s2); // false
console.log(s1 === s0); // false
console.log(s2 === s0); // true

而不使用new,直接调用String构造函数,返回的是一个基本类型字符串,效果与使用'hello'这种字符串字面量创建是一样的,用new返回的是一个包装类型的字符串

s1.foo = 'wscats';
s2.foo = 'wscats';
//console.log(s1);
console.log(s1.foo); // wscats
console.log(s2.foo); // undefined
posted on 2017-08-29 15:39  奶油小子  阅读(857)  评论(0)    收藏  举报