js中 instanceof 检测 字符串

it2025-10-13  17

如果单纯的将一个字符串赋给变量,虽然类型为string,但并不是String对象,没有创建实例。

var str = "newStr"; console.log(type of str); // true console.log(str instanceof String); // false

 而这种通过new一个String对象的方式是属于String,但是typeof在str原型链中找到最高级,new String()出来的属于对象,因此控制台打印结果为 object

var str = new String(); console.log(typo of str); // object console.log(str instanceof String); // true

 

最新回复(0)