Javascript - Calling a child method from inside a prototype method

Vic Cat o'Lick

Is it possible to call a regular method from a prototype method if it's been overridden? Note that in the given example the first eat() method is called from the body of the Animal function - it is the requirement.

Animal = function(){
    this.eat();
};

Animal.prototype.eat = function() {
    console.log("All animals eat");
};

Bear = function(){
    Animal.call(this);
    this.goToSleep = function(){
        console.log("Bears go to sleep after they eat");
    }
};

Bear.prototype = Object.create(Animal.prototype);

Bear.prototype.eat = function() {
    console.log("Bears eat honey");
    this.goToSleep(); //returns 'undefined is not a function'
    //How do I invoke this.goToSleep() ?
};

var winnie = new Bear();
Sean Vieira

The issue is that your bear doesn't have a goToSleep method when eat first gets invoked.

When you invoke Animal.call(this) in your Bear constructor it then calls eat - which is found on Bear.prototype and invoked. Then eat tries to invoke goToSleep but goToSleep is an instance method that you haven't added yet (remember, we are still on Animal.call we haven't reached this.goToSleep = function() yet).

Unless you have a good reason to, goToSleep should be a prototype method as well, just like eat:

Bear.prototype.goToSleep = function(){
  console.log("Bears go to sleep after they eat");
};

This will just work as you want it to.

Alternatively, if goToSleep has to be an instance method (because it needs access to some private state you create in your constructor), then simply switch the order of your Animal.call and this.goToSleep lines:

this.goToSleep = function() // etc.
Animal.call(this);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling Javascript prototype method from another method

From Javascript

Calling one prototype method inside another in javascript

From Javascript

Calling method using JavaScript prototype

From Dev

Calling prototype method inside another results in 'is not a function'

From Dev

Calling parent method from child?

From Dev

undefined function when calling javascript prototype method

From Dev

Calling a method inside another method JavaScript

From Dev

Calling prototype method from another object

From Dev

Calling a method inside a method

From Dev

Calling an object's prototype method from another object's method

From Dev

Java - Calling method from child of abstract class

From Dev

calling a parent UIViewController method from a child UIViewController

From Dev

Calling a method on Parent Component from Child Component

From Dev

Calling a method from child component in ionic 2

From Dev

Calling a parent method from outside the child

From Dev

Python calling extended child method from parent

From Dev

Calling child method from obj of parent class

From Dev

(javascript) Calling a method on an object inside an array

From Dev

Calling a method from inside of another class

From Dev

calling grandparent method from inside the grandchild component

From Dev

calling static method from inside the class

From Dev

Calling a method inside Activity from BroadcastReceiver

From Dev

Calling static method from inside a task

From Dev

Why does calling a method from base class calls the child method?

From

Calling child's method from parent's implementation method in golang

From Dev

React-Native - Calling Parent Method from Child Method

From Dev

PHP Calling method inside method

From Dev

Overriding prototype method and calling the original method

From Dev

Calling an inherited class method decorator inside child class

Related Related

  1. 1

    Calling Javascript prototype method from another method

  2. 2

    Calling one prototype method inside another in javascript

  3. 3

    Calling method using JavaScript prototype

  4. 4

    Calling prototype method inside another results in 'is not a function'

  5. 5

    Calling parent method from child?

  6. 6

    undefined function when calling javascript prototype method

  7. 7

    Calling a method inside another method JavaScript

  8. 8

    Calling prototype method from another object

  9. 9

    Calling a method inside a method

  10. 10

    Calling an object's prototype method from another object's method

  11. 11

    Java - Calling method from child of abstract class

  12. 12

    calling a parent UIViewController method from a child UIViewController

  13. 13

    Calling a method on Parent Component from Child Component

  14. 14

    Calling a method from child component in ionic 2

  15. 15

    Calling a parent method from outside the child

  16. 16

    Python calling extended child method from parent

  17. 17

    Calling child method from obj of parent class

  18. 18

    (javascript) Calling a method on an object inside an array

  19. 19

    Calling a method from inside of another class

  20. 20

    calling grandparent method from inside the grandchild component

  21. 21

    calling static method from inside the class

  22. 22

    Calling a method inside Activity from BroadcastReceiver

  23. 23

    Calling static method from inside a task

  24. 24

    Why does calling a method from base class calls the child method?

  25. 25

    Calling child's method from parent's implementation method in golang

  26. 26

    React-Native - Calling Parent Method from Child Method

  27. 27

    PHP Calling method inside method

  28. 28

    Overriding prototype method and calling the original method

  29. 29

    Calling an inherited class method decorator inside child class

HotTag

Archive