I was discussing Javascript OO strategies/patterns with a colleague, and from most tutorials I've read, the common way of establishing a prototype/inheritance relationship between two constructor functions (with arguments) is to assign a prototype to the child function.
e.g.:
var MyParentClass = function(arg1) { ... };
var MyChildClass = function(arg1, arg2) {
MyParentClass.apply(this, arguments);
...
};
MyChildClass.prototype = new MyParentClass;
One of the problems here is that the argument might be in a different order, or change names, etc., and the above example does require two (2) separate statements to establish the relationship (the apply() statement, and the .prototype statemetn).
My colleague suggested this approach instead:
var MyParentClass = function(arg1) { ... };
var MyChildClass = function(arg1, arg2) {
this.__proto__ = new MyParentClass(arg2);
...
};
This approach is shorter and gives more flexibility towards which arguments are passed to the parent constructor.
Unless I'm missing something, seems like this should be the de-facto pattern for establishing inheritance between JavaScript classes, so I'm curious as to why I've never come across that pattern in all JavaScript OO tutorials so far. Can anyone let me know if the above strategy has any downside?
__proto__is non-standard, deprecated and should not be used.__proto__separately on each instance soinstance1.__proto__ !== instance2.__proto__.