0

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?

7
  • 3
    Because __proto__ is non-standard, deprecated and should not be used. Commented Jul 9, 2014 at 8:08
  • Also because you're setting __proto__ separately on each instance so instance1.__proto__ !== instance2.__proto__. Commented Jul 9, 2014 at 8:14
  • @JamesAllardice in that's case it's desired behaviour if I'm correct, we don't want two instances pointing to the same prototype instance. For example if Parent is Person, and Child is Employee, we want each Employee's prototype object to be a separate Person instance as well. Am I misunderstanding something however? Commented Jul 9, 2014 at 8:15
  • 1
    A protoype defines reusable beahavior for Objects of the same domain. So it would be confusing to me if to instances of the same prototype (domain) don't share the same prototype. Commented Jul 9, 2014 at 8:22
  • For example: All Objects in JS are children of Object.prototype and they all share the same prototype Object.prototype. Commented Jul 9, 2014 at 8:25

1 Answer 1

3

You can address the Problem with passing parameters to the parent-constructor- function as follows:

Instead of

MyChildClass.prototype = new MyParentClass;

you can write

MyChildClass.prototype = Object.create(MyParentClass.protoype)

in that way you can assign the prototype without a new instantiation of MyParentClass

If you want to pass parameters from the constructorFunction from MyChildClass to MyParentClass you can do that like that:

function MyChildClass(arg1) {
    MyParentClass.call(this, arg1);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.