Different Ways Of Extending Classes In Node.js
Solution 1:
I was looking at the source-code of expressjs
You might also want to have a look at this question about how app is supposed to work.
What are the differences between
utils-mergeas 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-mergeMerges the properties from a source object into a destination object.
Inherit the prototype methods from one constructor into another. The prototype of constructor will be set to a new object created from superConstructor.
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.
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 };
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;If so, will that still work even if
util.inheritis used?app.request = util.inherit(app, req) // Or something like that?
No, really not.
jshint says
__proto__is depricated.
Yes, you should use Object.create instead. But the nonstandard __proto__ will probably be kept for compatibility.
Additionally I also saw this?
varres=module.exports = { __proto__: http.ServerResponse.prototype };Could this be?
varres=module.exports = util.inherits...??
No, again that's a case for Object.create:
var res = module.exports = Object.create(http.ServerResponse.prototype);
Solution 2:
You can use ES6 in Node.js.
classmyClass() {
this.hi = function(){"hello"};
}
classmyChildextendsmyClass() {
}
var c = newmyChild(); c.hi(); //hello
Post a Comment for "Different Ways Of Extending Classes In Node.js"