要用ObjectSetPrototypeOf(A.prototype,B.prototype);和ObjectSetPrototypeOf(A,B)继承实现。"/>
为什么Node.js要用ObjectSetPrototypeOf(A.prototype,B.prototype);和ObjectSetPrototypeOf(A,B)继承实现。
我发现Node.js是这样实现继承的,比如。https:/githubnodejsnodeblobmasterlib_http_server.js。
ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype); // line 174
ObjectSetPrototypeOf(ServerResponse, OutgoingMessage); // line 175
而不是只设置prototype。
ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);
回答如下:这样父类的静态方法就被子类继承了,因为他们已经把父类的构造函数变成了子类构造函数的原型。
原生 class
语法也是如此。
class Parent {
static example() {
console.log("Parent.example");
}
}
class Child extends Parent {
}
console.log(Child.example()); // "Parent.example"
console.log(Child.hasOwnProperty("example")); // false
console.log("example" in Child); // true, because it's inherited
console.log(Object.getPrototypeOf(Child) === Parent); // true
更多推荐
为什么Node.js要用ObjectSetPrototypeOf(A.prototype,B.prototype);和ObjectSetPrototypeOf(
发布评论