【问题标题】:Different ways of extending classes in node.js在 node.js 中扩展类的不同方法
【发布时间】: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


【解决方案1】:

我在看expressjs的源代码

您可能还想看看this question,了解app 应该如何工作。

utils-merge有什么区别 如上所述,只是做类似的事情:

var util = require('util');
....
util.inherit(app, proto);
util.inherit(app, EventEmitter);

他们在做完全不同的事情:

utils-merge
将源对象的属性合并到目标对象中。

util.inherits

将原型方法从一个构造函数继承到另一个构造函数。构造函数的原型将被设置为从创建的新对象 超级构造器。

另请阅读theirsources

app(虽然出于奇怪的原因是一个函数)不是构造函数,但应该是一个(普通)对象 - 一个由createApplication 创建的实例。所以这里没有办法做“类继承”。而且你不能在同一个构造函数上多次使用utils.inherits,因为它确实覆盖它的.prototype属性。

相反,mixin 函数将简单地复制 proto 中的所有属性,然后将 EventEmitter.prototype 中的所有属性复制到 app 对象。

这还在尝试扩展属性吗?我有点迷路了:

app.request = { __proto__: req, app: app }; // what is the equivalent for this in util?
app.response = { __proto__: res, app: app };

为此使用本机 Object.create 函数:

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。但是nonstandard __proto__ 可能会被保留以保持兼容性。

另外我也看到了这个?

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);

【讨论】:

  • 还有一个问题,这个怎么样? this.request.__proto__ = parent.request;
  • 什么是parentthis.request 最初是如何创建的?但不是,you should really not change the __proto__ 在创建对象之后。
  • 在 expressjs 应用程序代码上。我不太了解代码中的所有继承。
  • 啊,你的意思是this file。是的,他们确实在那里做着丑陋的事情,显然是为了在不修改它们的情况下扩展原生对象。但是,为什么您需要了解所有代码?只需使用库。
  • 因为this。从技术上讲,尝试为 net/tcp 协议创建一个简单的类似协议。而不是http。
【解决方案2】:

您可以在 Node.js 中使用 ES6

class myClass() {
  this.hi = function(){"hello"};

}

class myChild extends myClass() {

}

var c = new myChild(); c.hi(); //hello

【讨论】:

  • 虽然这需要--harmony 对吧?此外,如果我将他们的代码重写为更新的规范,我仍然感到困惑,它会是什么样子。我相信 proto 不会再存在了,因为 node.js 现在有 util.inherit。但我无法理解如何用 util 编写它。
  • 如果有的话,class 关键字需要小写。我也看不出这是如何回答这个问题的。
  • v8 中还没有实现类
  • 但是你可以用 Traceur no 填充主题吗?
猜你喜欢
  • 2016-08-12
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多