为什么Node.js要用ObjectSetPrototypeOf(A.prototype,B.prototype);和ObjectSetPrototypeOf(A,B)继承实现。

编程入门 行业动态 更新时间:2024-10-11 13:21:25

为什么Node.js<a href=https://www.elefans.com/category/jswz/34/1770424.html style=要用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(

本文发布于:2024-05-13 12:59:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要用   js   Node   ObjectSetPrototypeOf   prototype

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!