请教下Node.js源码中的ObjectSetPrototypeOf的使用问题
 发布于 5 年前  作者 assmdx  5107 次预览  最后一次回复是 5 年前  来自 问答 

我发现Node里面,很多这样实现继承的代码: https://github.com/nodejs/node/blob/master/lib/_http_server.js

	ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);   // 174行
	ObjectSetPrototypeOf(ServerResponse, OutgoingMessage);  // 175行

为什么要这样设置呢? 直接设置一个不行吗?比如:

	ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);
2 回复
hyj1991

第一行是继承原型方法,就是 new 出来后能调用的, 第二行是继承静态方法,相当于直接 ServerResponse.xxx 这是兼容老的写法