1

my question is: why there is difference between all 3 ways of defining inheritance ? and if the first snippet is the most correct one how people before 2015 dealt with new keyword ?

  1. The first snippet the object y is instance of gorilla and it has the prototype and methods of animal, and animal inherits from object prototype
    NOTE:that name, age are defined on the object itself
  2. The second snippet is the same as first snippet but the difference is it has additional properties on the Animal prototype name,age and they both have undefined value duo to new call.
  3. The third snippet is whole different it has the prototype of Object and it is like it moved every method from the animal prototype to itself.

Consider Looking at these snippets

function Animal(name, age) {
  this.name = name;
  this.age = age;
}

Animal.prototype.eat = function () {
  console.log(`${this.name} is eating!`);
};

function Gorilla(name, age, action) {
  Animal.call(this, name, age);
}

Gorilla.prototype = Object.create(Animal.prototype);
Gorilla.prototype.constructor = Gorilla;

var y = new Gorilla("morphy", 50);
console.log(y);

above snippet picture

function Animal(name, age) {
  this.name = name;
  this.age = age;
}

Animal.prototype.eat = function () {
  console.log(`${this.name} is eating!`);
};

function Gorilla(name, age, action) {
  Animal.call(this, name, age);
}

Gorilla.prototype = new Animal();
Gorilla.prototype.constructor = Gorilla;

var y = new Gorilla("morphy", 50);
console.log(y);

above snippet picture

function Animal(name, age) {
  this.name = name;
  this.age = age;
}

Animal.prototype.eat = function () {
  console.log(`${this.name} is eating!`);
};

function Gorilla(name, age, action) {
  Animal.call(this, name, age);
}

Gorilla.prototype = Animal.prototype;
Gorilla.prototype.constructor = Gorilla;

var y = new Gorilla("morphy", 50);
console.log(y);

enter image description here

Boudy hesham
  • 332
  • 1
  • 2
  • 12
  • 1
    Isn't this more a question of inheritance than polymorphism? – Ted Brownlow Jan 05 '21 at 20:30
  • yea sry i missed up – Boudy hesham Jan 05 '21 at 20:37
  • "*why inheritance has no standard way in javascript*" - it has (since 2015): `class Gorilla extends Animal { }`. Use a more modern tutorial. – Bergi Jan 05 '21 at 20:41
  • Would be interesting to know the source of this revelation about "the three ways". Use of `Animal.call()` is questionable and not necessary if you craft inheritance based upon a design (rather than on-the-fly). Setting the prototype first allows you to construct an Gorilla and set property values with `this.` – Randy Casburn Jan 05 '21 at 20:42
  • @Bergi - you'll get a bunch of pushback from the React fans that insist that "_hooks_" are more important than food - hence they won't use the `class` keyword. :-) – Randy Casburn Jan 05 '21 at 20:44
  • 1
    @Boudyhesham The third way is outright wrong and doesn't work. The second way is what people did before the introduction of `Object.create` in ES5 (2011), and has the drawbacks you noticed. The first way is the proper one. – Bergi Jan 05 '21 at 20:46
  • @RandyCasburn Sure, but react hooks don't provide prototypical inheritance. (And I'd even argue that they don't do inheritance at all, but rather composition - which is good in their use case, but not important here). React fans wouldn't use constructors and `new` either. – Bergi Jan 05 '21 at 20:48
  • why this man closed my question as duplication it has nothing to do with benifits of object.create i had one more question well thanks to u @Bergi – Boudy hesham Jan 05 '21 at 21:22
  • @RandyCasburn so you're saying that Animal.call() is not necessary to establish a super call between the 2 ? – Boudy hesham Jan 05 '21 at 21:24
  • @Boudyhesham While the title of the canonical topic might not fit yours, it is an exact duplicate, discussing all three approaches you mentioned, and should answer all your questions regarding them. Which question does it not answer? – Bergi Jan 05 '21 at 21:27
  • the last one u asnwered the third one is not a proper way of defining inheritance – Boudy hesham Jan 05 '21 at 21:28
  • Please read the QA again then. It's discussed under "*Some say that `Dog.prototype = Animal.prototype;` can also work. So now I'm totally confused*" – Bergi Jan 05 '21 at 21:29
  • wasn't this ur comment ! `The third way is outright wrong and doesn't work. The second way is what people did before the introduction of Object.create in ES5 (2011), and has the drawbacks you noticed. The first way is the proper one` – Boudy hesham Jan 05 '21 at 21:32
  • @Boudyhesham - not if you use setters/getters, but never mind as I'm so confused about which version we're discussing - unless you are maintaining code from 2012, do what Bergi says and use `extends` + `super()` - problem solved. – Randy Casburn Jan 05 '21 at 22:09

0 Answers0