Possible Duplicate:
What is the reason to use the ‘new’ keyword here?
I am studying Mongoose (what a beautiful piece of software...) and am seeing this:
function Model (doc, fields, skipId) {
Document.call(this, doc, fields, skipId);
};
/*!
* Inherits from Document.
*/
Model.prototype.__proto__ = Document.prototype;
Wow, that's the easiest way to set inheritance I've ever seen. I know it cannot be done with browsers, but server side... it looks like a winner:
And that's it!
Is this possibly the cleanest, easiest way to implement inheritance on the server's side? I am asking because I am in love with it, and am wondering if I am missing some limitations/problems...?
Well, some browser's franeworks used to mimic this by having A.prototype = new B()
, but this is a bit hacky :) One important gain of both ways is that monkey-patching parent class enabled descendants to use new/changed methods of the parent (which is not the case with A.prototype = $.extend({}, B.prototype)
and similar hacks).
As for the described approach, it definitely looks cleaner, so I would vote for "Yes"
As far as I can tell, that can be done in the client as well like:
Model.prototype = new Document();
Model.prototype.constructor = Model;