如何解决Javascript:“ Object.setPrototypeOfChild.prototype,Parent.prototype”与“ Child.prototype = Object.createParent.prototype”
例如,在这段非常原始和简化的代码中:
function Parent() { }
function Child() { }
Object.setPrototypeOf(Child.prototype,Parent.prototype); // OR: "Child.prototype.__proto__ = Parent.prototype;" (the result is totally identical)
与之相对:
function Parent() { }
function Child() { }
Child.prototype = Object.create(Parent.prototype);
然后我按照以下方式检查这两段代码:
console.log(child instanceof Child); // true for both examples
console.log(child instanceof Parent); // true for both examples
console.log(Parent.prototype.isPrototypeOf(child)); // true for both examples
console.log(Child.prototype.isPrototypeOf(child)); // true for both examples
但是:对接下来的两行进行检查会得出不同的结果-
console.log(JSON.stringify(Object.getOwnPropertyNames(Child.prototype),null,2)); // ["constructor"] vs []
console.log(child.__proto__); // a constructor property is shown (on the first place) only in the first case and no constructor property – in the second case
-仅在第一种情况下显示“父”构造函数(请参见所附图像)。 是什么原因导致这种差异?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。