首页 > js中,字符串字面量和通过构造函数得到字符串有什么本质区别嘛?

js中,字符串字面量和通过构造函数得到字符串有什么本质区别嘛?

代码示例如下:var foo = "hello"; var bar = new String("hello");的区别在哪里?


var foo = 'hello';
foo.newPro = 'pro';  // will be ignored

console.log(typeof foo);  // string
console.log(foo instanceof String); // false
console.log(foo.newPro); // undefined

var bar = new String('hello'); 
bar.newPro = 'pro';  // will be OK
console.log(typeof bar);    // object
console.log(bar instanceof String);    // true
console.log(bar.newPro); // pro
【热门文章】
【热门文章】