Javascript node.js中扩展类的不同方式
扩展node.js或javascript中的原型类。(js newb) 我在查看expressjs的源代码时看到:Javascript node.js中扩展类的不同方式,javascript,node.js,Javascript,Node.js,扩展node.js或javascript中的原型类。(js newb) 我在查看expressjs的源代码时看到: Utils merge似乎是一个外部模块。上面提到的和仅仅做以下事情有什么区别: var util = require('util'); .... util.inherit(app, proto); util.inherit(app, EventEmitter); var util = require('util'); .... util.inherit(app, proto);
Utils merge
似乎是一个外部模块。上面提到的和仅仅做以下事情有什么区别:
var util = require('util');
....
util.inherit(app, proto);
util.inherit(app, EventEmitter);
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 };
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)
app.request = util.inherit(app, req) // Or something like that?
或者类似的?jshint说,\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
是去润滑的
另外我也看到了这个
var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
可能是这样吗
var res = module.exports = util.inherits...??
var res = module.exports = util.inherits...??
您可以在Node.js中使用
ES6
class myClass() {
this.hi = function(){"hello"};
}
class myChild extends myClass() {
}
var c = new myChild(); c.hi(); //hello
我在看expressjs的源代码
您可能还想了解一下app
的工作原理
utils merge
如上所述,只需执行以下操作:
var util = require('util');
....
util.inherit(app, proto);
util.inherit(app, EventEmitter);
var util = require('util');
....
util.inherit(app, proto);
util.inherit(app, EventEmitter);
他们在做完全不同的事情:
将源对象中的属性合并到目标对象中 将原型方法从一个构造函数继承到另一个构造函数中。构造函数的原型将设置为从中创建的新对象 超级建设者 也读
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 };
app.request = { __proto__: req, app: app }; // what is the equivalent for this in util?
app.response = { __proto__: res, app: app };
为此使用本机函数:
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)
app.request = util.inherit(app, req) // Or something like that?
不,真的没有
jshint说,\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
是去润滑的
是的,您应该使用对象。请改为创建。但是,为了兼容性,可能会保留
另外我也看到了这个
var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
可能是这样吗
var res = module.exports = util.inherits...??
var res = module.exports = util.inherits...??
不,这也是对象的情况。创建:
var res = module.exports = Object.create(http.ServerResponse.prototype);
虽然这需要--harmony
对吗?此外,我仍然很困惑,如果我将他们的代码重写为新的规范,它会是什么样子。我相信proto将不再存在,因为node.js现在有util.inherit。但是我不明白如何用util编写它。如果真的这样做的话,class
关键字需要小写。我也看不出这是如何回答这个问题的。v8 YetB中也没有实现类,但您可以使用Traceur no填充主题?您可以使用util.inherit
每个构造函数只使用一次!那么除了:mixin(app,proto)还有什么替代方案吗;mixin(app,EventEmitter.prototype)代码>什么是应用程序
?什么是proto
?或者它的替代方案:app.request={{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu代码>因为jshint说不推荐使用。@Bergi可以找到它。是express框架库的一部分。还有一个问题,这个怎么样<代码>this.request.\uuuuu proto\uuuuuu=parent.request代码>什么是父级
?这个请求最初是如何创建的?但是没有,在创建对象之后。它在expressjs应用程序代码中。我不太理解代码中所有的继承。啊,你的意思是。是的,它们确实在做一些丑陋的事情,显然是为了在不修改它们的情况下扩展本机对象。但是,为什么您需要理解所有代码?就用图书馆吧,因为。从技术上讲,尝试为net/tcp协议创建一个简单的类似协议。而不是http。