ser*_*0ne 31 javascript string instanceof string-literals
在JavaScript中,我可以通过以下方式声明字符串;
var a = "Hello World";
var b = new String("Hello World");
Run Code Online (Sandbox Code Playgroud)
但是a不是String的实例......
console.log(a instanceof String); //false;
console.log(b instanceof String); //true;
Run Code Online (Sandbox Code Playgroud)
那么如何找到类型或" instanceof"字符串文字?
可以强制JavaScript new String()为每个字符串文字创建一个?
Art*_*dod 65
用typeof "foo" === "string"而不是instanceof.
小智 5
无需编写new String()创建新字符串。当我们编写var x = 'test';语句时,它会x根据原始数据类型创建字符串。x我们无法像处理对象文字那样将自定义属性附加到此。IE。x.custom = 'abc'; x.custom将给出未定义的值。因此,根据我们的需要,我们需要创建该对象。将使用Object 而不是字符串new String()创建一个对象。typeof()我们可以向该对象添加自定义属性。