Javascript中String()和new String()的区别
在JavaScript中,使用Javascript中String()和new String()的区别,javascript,string,Javascript,String,在JavaScript中,使用String()和newstring()有什么区别吗 在不使用new的情况下使用String()构造函数可以为您提供所传递参数的字符串(原语)值。这就像必要时将参数装箱到本机对象中(如数字或布尔值),然后对其调用.toString()。(当然,如果传递一个普通对象引用,它只会调用.toString()) 调用新字符串(某物)生成字符串实例对象 通过console.log(),结果看起来是一样的,因为它只是从传递给它的字符串实例中提取基本字符串 所以:只需简单地Str
String()
和newstring()
有什么区别吗
在不使用new
的情况下使用String()
构造函数可以为您提供所传递参数的字符串(原语)值。这就像必要时将参数装箱到本机对象中(如数字或布尔值),然后对其调用.toString()
。(当然,如果传递一个普通对象引用,它只会调用.toString()
)
调用新字符串(某物)
生成字符串实例对象
通过console.log()
,结果看起来是一样的,因为它只是从传递给它的字符串实例中提取基本字符串
所以:只需简单地String()
返回一个字符串原语<代码>新字符串(xyz)返回由字符串构造函数构造的对象
很少需要显式构造字符串实例。在非构造函数上下文中从
String
调用返回的字符串(即,不使用new
关键字)是基本字符串
使用new String()
(构造函数模式)创建的字符串是一个对象,可以在其中存储属性
展示差异:
var strPrimitive=String('word');
strPrimitive.prop=“bar”;
console.log(strPrimitive.prop);//未定义
var strObject=新字符串('word');
strObject.prop=“bar”;
console.log(strObject.prop);//bar
除了已经提供的好答案之外,这里还有一个例子:
var x=String('word');
console.log(x类型);//“字符串”
变量y=新字符串(“单词”);
console.log(y类型);//“object”
String()返回一个字符串原语,new String()返回一个对象字符串。这对代码有一些实际影响
var x = String('word');
console.log(typeof x); // "string"
var y = new String('word');
console.log(typeof y); // "object"
// compare two objects !!!
console.log(new String('') === new String('')) // false!!!
// compare with string primitive
console.log('' == String('')) // true
console.log('' === String('')) // true
//compare with string Object
console.log('' == new String('')) // true
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
console.log('' === new String('')) // false !!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// instance of behavior
console.log(x instanceof String); // false
console.log(x instanceof Object); // false
// please note that only new String() is a instanceOf Object and String
console.log(y instanceof String); // true
console.log(y instanceof Object); // true
//valueOf behavior
console.log('word' == x.valueOf()); // true
console.log('word' === x.valueOf()); // true
console.log('word' == y.valueOf()); // true
console.log('word' === y.valueOf()); // true
//create smart string
var superString = new String('Voice')
superString.powerful = 'POWERFUL'
String.prototype.shout = function () {
return `${this.powerful} ${this.toUpperCase()}`
};
console.log(superString.shout()) //"POWERFUL VOICE"
谢谢@Pointy。所以字符串('something')和'something'是一样的?我刚刚看到了这个答案:是的,特别是用字符串原语调用plain
String()
,会返回该字符串原语。这是一个幂等式no-op。更实际地说,newstring(“word”)==newstring(“word”)
将是false
,因为这是两个不同的对象,而String(“word”)==String(“word”)
将是true
,因为您正在比较原语hank you@Mamun。添加属性是您选择使用new String()的主要原因吗?@cham,因为它们之间除了类型之外没有其他区别,所以当我需要它们作为对象时,我会使用new String()
。。谢谢。请注意,字符串基元类型的字符串对象包装器主要是出于历史原因而存在的,实际用途有限,效率可能低于使用基本字符串值。另见,谢谢。我还添加了一点,newstring(“”)==newstring(“”)
是false
@米尔·伊斯梅利补充道:D谢谢你!
var x = String('word');
console.log(typeof x); // "string"
var y = new String('word');
console.log(typeof y); // "object"
// compare two objects !!!
console.log(new String('') === new String('')) // false!!!
// compare with string primitive
console.log('' == String('')) // true
console.log('' === String('')) // true
//compare with string Object
console.log('' == new String('')) // true
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
console.log('' === new String('')) // false !!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// instance of behavior
console.log(x instanceof String); // false
console.log(x instanceof Object); // false
// please note that only new String() is a instanceOf Object and String
console.log(y instanceof String); // true
console.log(y instanceof Object); // true
//valueOf behavior
console.log('word' == x.valueOf()); // true
console.log('word' === x.valueOf()); // true
console.log('word' == y.valueOf()); // true
console.log('word' === y.valueOf()); // true
//create smart string
var superString = new String('Voice')
superString.powerful = 'POWERFUL'
String.prototype.shout = function () {
return `${this.powerful} ${this.toUpperCase()}`
};
console.log(superString.shout()) //"POWERFUL VOICE"