【发布时间】:2011-07-20 21:18:12
【问题描述】:
在 Node.js 的 Express 模块的代码中,我遇到了这一行,为服务器设置继承:
Server.prototype.__proto__ = connect.HTTPServer.prototype;
我不确定这是做什么的 - MDC 文档 (https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited#prototype_and_proto) 似乎说我可以做吧:
Server.prototype = connect.HTTPServer.prototype;
确实,我做了这个测试:
var parent = function(){}
parent.prototype = {
test: function(){console.log('test')};
}
var child1 = function(){};
child1.prototype = parent.prototype;
var instance1 = new child1();
instance1.test(); // 'test'
var child2 = function(){};
child2.prototype.__proto__ = parent.prototype;
var instance2 = new child2();
instance2.test(); // 'test'
看起来是一样的?所以是的,我想知道设置 object.prototype.__proto 的目的是什么。谢谢!
【问题讨论】:
标签: javascript node.js