Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways of extending classes in node.js

Extending prototypical classes in node.js or javascript in general. (js newb)

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

If so, will that still work even if util.inherit is used?

app.request = util.inherit(app, req)

Or something like that? jshint says __proto__ is depricated.


Additionally I also saw this?

var res = module.exports = {
  __proto__: http.ServerResponse.prototype
};

Could this be?

var res = module.exports = util.inherits...??
like image 226
majidarif Avatar asked Feb 13 '23 11:02

majidarif


1 Answers

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-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-merge
Merges the properties from a source object into a destination object.

util.inherits

Inherit the prototype methods from one constructor into another. The prototype of constructor will be set to a new object created from superConstructor.

Also read their sources!

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.inherit is 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?

var res = module.exports = {
  __proto__: http.ServerResponse.prototype
};

Could this be?

var res = module.exports = util.inherits...??

No, again that's a case for Object.create:

var res = module.exports = Object.create(http.ServerResponse.prototype);
like image 79
Bergi Avatar answered Feb 15 '23 05:02

Bergi