Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中String()和new String()的区别_Javascript_String - Fatal编程技术网

Javascript中String()和new String()的区别

Javascript中String()和new String()的区别,javascript,string,Javascript,String,在JavaScript中,使用String()和newstring()有什么区别吗 在不使用new的情况下使用String()构造函数可以为您提供所传递参数的字符串(原语)值。这就像必要时将参数装箱到本机对象中(如数字或布尔值),然后对其调用.toString()。(当然,如果传递一个普通对象引用,它只会调用.toString()) 调用新字符串(某物)生成字符串实例对象 通过console.log(),结果看起来是一样的,因为它只是从传递给它的字符串实例中提取基本字符串 所以:只需简单地Str

在JavaScript中,使用
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()返回一个对象字符串。这对代码有一些实际影响

  • 使用String()返回'true',其他原语同时使用==和===运算符
  • 使用String()会生成一个基元,因此它不能使用“instanceOf”方法检查其类型。只能使用“typeof”运算符检查值类型
  • 将new String()与“instanceOf”方法一起用于字符串或对象原型-两者都断言为true
  • 使用new String()将仅通过调用valueOf()方法返回带有字符串原语的“true”。String()也有此方法,与相同值的字符串进行比较时返回true
  • 使用new 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"