0

i was answering to a question where i encountered this problem In the following code how the child's prototype can be set to parent using object.create() method.I can do it using

child.prototype=new Parent();

But i want to do it using object.create.using child.prototype=Object.create(Parent) didn't set the prototype to Parent

enter image description here

function Parent() {
   this.parentFunction = function(){
      console.log('parentFunction');
   }
}
Parent.prototype.constructor = Parent;

function Child() {

   this.parentFunction = function() {

      this.constructor.prototype.parentFunction.call(this);
      console.log('parentFunction from child');
   }
}
Child.prototype = Object.create(Parent);
Child.prototype.constructor = Child;

var child = new Child();
console.dir(child);
child.parentFunction();
Community
  • 1
  • 1
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • 1
    `Object.create` creates a new object with the given prototype. You already have an object, you just want to change the prototype of it, so `Object.create` seems like the wrong method to use to just change the prototype. – adeneo Jan 15 '15 at 16:25
  • `Child.prototype = Object.create(Parent.prototype);`. Although there is no value in doing that because you didn't add anything to `Parent.prototype`. There are other things you are missing. Have a look at [Benefits of using `Object.create` for inheritance](http://stackoverflow.com/q/17392857/218196). – Felix Kling Jan 15 '15 at 16:28
  • @FelixKling thanks !! but can you please elaborate!! I have not defined parent's prototype yet.Statement seems confusing – AL-zami Jan 15 '15 at 16:31
  • 1
    Have a look at the other question, I think my answer explains everything you need to know. `Object.create(Parent)` would create a new object that has the **function** `Parent` in its prototype chain. That's not what you want. Ideally, `parentFunction` would be defined on `Parent.prototype`. – Felix Kling Jan 15 '15 at 16:31
  • I suggest you to use Parent.call(this) within the Child-Constructor to be able to heriate a parent method in instance inheritance way. – Blauharley Jan 15 '15 at 16:33

1 Answers1

0

Two issues:

  1. The first parentFunction you define is in the constructor for Parent, not the prototype. So Parent.prototype.parentFunction isn't defined. Instead, there is a separate copy of the parentFunction for ever instance of Parent.

  2. In the Child constructor, this.constructor.prototype refers to the prototype of Child, not the prototype of Parent. If you want the Parent prototype, that's accessible with this.prototype.prototype.

I minimally modified your code so that calling parentFunction on the child calls parentFunction on the parent:

function Parent() {

}

Parent.prototype.parentFunction = function(){
      console.log('parentFunction in parent');
}


Parent.prototype.constructor = Parent;

function Child() {

}

Child.prototype = Object.create(Parent);
Child.prototype.parentFunction = function() {
      this.prototype.parentFunction.call();
      console.log('parentFunction from child');
};
Child.prototype.constructor = Child;

var child = new Child();
console.dir(child);
child.parentFunction();
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
  • it does the same as my code but in a different way?Can you point out why the code i've provided fails?? – AL-zami Jan 15 '15 at 20:45
  • 1
    Sure. I explained at the top of my answer. In your code, there is no `parentFunction` of the `Parent` prototype because you are assigning to `this` in the constructor. `this` is a reference to the current object, *not* to the prototype, so the prototype is never assigned the `parentFunction` property. In my version I assigned the property to the prototype. The second issue is that your code to access the prototype of the prototype of instances of `Child` was incorrect: should be `this.prototype` instead of `this.construtor.prototype`. – Max Heiber Jan 15 '15 at 21:27