【发布时间】:2014-07-17 17:21:18
【问题描述】:
通常在 node.js 或 javascript 中扩展原型类。 (js newb)
我在看expressjs的源代码,看到this:
var mixin = require('utils-merge');
....
mixin(app, proto);
mixin(app, EventEmitter.prototype);
Utils-merge 似乎是一个外部模块。上面和只是做类似的事情有什么区别:
var util = require('util');
....
util.inherit(app, proto);
util.inherit(app, EventEmitter);
这还在尝试扩展属性吗?我有点迷路了:
app.request = { __proto__: req, app: app }; // what is the equivalent for this in util?
app.response = { __proto__: res, app: app };
如果是这样,即使使用util.inherit,它仍然有效吗?
app.request = util.inherit(app, req)
或者类似的东西? jshint 说 __proto__ 被贬低了。
另外我也看到了这个?
var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
会这样吗?
var res = module.exports = util.inherits...??
【问题讨论】:
-
每个构造函数只能使用
util.inherit一次! -
那么有什么替代方法:
mixin(app, proto); mixin(app, EventEmitter.prototype); -
app到底是什么?proto是什么? -
或替代方法:
app.request = { __proto__: req, app: app };因为__proto__jshint 表示已弃用。 -
@Bergi 可以找到here。是 express 框架库的一部分。
标签: javascript node.js