在node.js中扩展类的不同方法 [英] Different ways of extending classes in node.js
问题描述
一般在node.js或javascript中扩展原型类。 (js newb)
Extending prototypical classes in node.js or javascript in general. (js newb)
我正在查看expressjs的源代码并看到这个:
I was looking at the source-code of expressjs and saw this:
var mixin = require('utils-merge');
....
mixin(app, proto);
mixin(app, EventEmitter.prototype);
Utils-merge
好像是一个外部模块。上面有什么区别,只是做了类似的事情:
Utils-merge
seems like an external module. What are the difference of above and just doing something like:
var util = require('util');
....
util.inherit(app, proto);
util.inherit(app, EventEmitter);
这还在尝试扩展属性吗?我很善良 - 迷失在这里:
And is this still trying to extend properties? I'm kind-a lost here:
app.request = { __proto__: req, app: app }; // what is the equivalent for this in util?
app.response = { __proto__: res, app: app };
如果是这样,即使 util.inherit $仍然有效是否使用了c $ c>?
app.request = util.inherit(app, req)
或类似的东西? jshint说 __ proto __
已被删除。
Or something like that? jshint says __proto__
is depricated.
另外我也看到了这个?
var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
这可能是吗?
var res = module.exports = util.inherits...??
推荐答案
我在看expressjs的源代码
I was looking at the source-code of expressjs
您可能还想查看这个问题关于如何 app
应该有效。
You might also want to have a look at this question about how app
is supposed to work.
如上所述
utils-merge
之间有什么区别,只是做了类似的事情:
What are the differences between
utils-merge
as above and just doing something like:
var util = require('util');
....
util.inherit(app, proto);
util.inherit(app, EventEmitter);
他们做的事情完全不同:
They're doing totally different things:
utils-合并
将源对象中的属性合并到目标对象中。
util.inherits
将原型方法从一个构造函数继承到另一个构造函数。构造函数的原型将设置为从
superConstructor创建的新对象。
app
(由于奇怪的原因是一个函数)不是构造函数,但应该是一个(普通)对象 - 由<$ c $生成的实例 C> createApplication 。所以这里没有办法做类继承。并且你不能在同一个构造函数上多次使用 utils.inherits
,因为它覆盖 .prototype
它的属性。
app
(while being a function for odd reasons) is not a constructor, but is supposed to be a (plain) object - an instance made by createApplication
. So there's no way to do "class inheritance" here. And you cannot use utils.inherits
multiple times on the same constructor anyways, as it does overwrite the .prototype
property of it.
相反, mixin
函数只会复制<的所有属性code> proto 然后从 EventEmitter.prototype
到 app
的所有属性对象。
Instead, that mixin
function will simply copy all the properties from proto
and then all properties from EventEmitter.prototype
to the app
object.
这是否还在尝试扩展属性?我很善良 - 迷失在这里:
And is this still trying to extend properties? I'm kind-a lost here:
app.request = { __proto__: req, app: app }; // what is the equivalent for this in util?
app.response = { __proto__: res, app: app };
使用原生 Object.create
功能:
Use the native Object.create
function for this:
app.request = Object.create(req);
app.request.app = app;
app.response = Object.create(res);
app.response.app = app;
如果是这样,即使
util.inherit
?
app.request = util.inherit(app, req) // Or something like that?
不,真的没有。
jshint说
__ proto __
已被删除。
是的,您应该使用 Object.create
。但非标准 __ proto__
可能会保留兼容性。
Yes, you should use Object.create
instead. But the nonstandard __proto__
will probably be kept for compatibility.
此外我还看到了这个?
Additionally I also saw this?
var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
这可能是吗?
var res = module.exports = util.inherits...??
不,这是的情况Object.create
:
var res = module.exports = Object.create(http.ServerResponse.prototype);
这篇关于在node.js中扩展类的不同方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!