Javascript 字符串构造函数函数(String.\uuuu proto\uuuu)的原型是什么?
现在,为什么Javascript 字符串构造函数函数(String.\uuuu proto\uuuu)的原型是什么?,javascript,inheritance,Javascript,Inheritance,现在,为什么 var mString = new String('A'); console.log(typeof mString); // object console.log(mString instanceof String); // true console.log(mString instanceof Object); // true console.log(mString.__proto__ === String.prototype); // true console.log
var mString = new String('A');
console.log(typeof mString); // object
console.log(mString instanceof String); // true
console.log(mString instanceof Object); // true
console.log(mString.__proto__ === String.prototype); // true
console.log(mString.__proto__.__proto__ === Object.prototype); // true
而不是
console.log(String.__proto__.__proto__ === Object.prototype); // true
在原型链上行走时
字符串和对象原型之间是什么?当使用对象的
实例时,表示变量是对象
或“扩展”对象
如前所述,“instanceof操作符测试构造函数的prototype属性是否出现在对象的prototype链中的任何位置。”
所以mString.\uuuu proto\uuuu
不等于Object.prototype
,但它Object.prototype
在原型链上确实更高,这就是它们不相等的原因。字符串原型继承对象原型
console.log(String.__proto__ === Object.prototype); // false
它是一个函数的原型,因为字符串是一个构造函数。在字符串实例和对象原型之间是字符串原型。不要使用不推荐的
。\uuuuuuuuuuuuuuuuuuuuu
getter。请改用Object.getPrototypeOf
。值得注意的是,这不仅适用于字符串,而且适用于所有类。例如(类A{})。\uuuu proto\uuuu===Function.prototype
是真的,因为类只是函数构造函数的语法糖。太棒了。谢谢,伙计们。
Function.prototype === String.__proto__ //true