Search code examples
javascriptinheritanceprototypejspolymorphism

Call base prototype function


I have the following 2 objects:

function circle(radius){
    this.radius = radius;    
    this.foo = function (){
        return "circle foo";};    
    return true;}

function pizza(){
    this.foo = function (){
        return "pizza foo";};
    return true;}

pizza.prototype = new circle(9);

When I do the following

var foo = myPizza.foo();

It prints the following as expected:

pizza foo

How can I activate the base class and print "circle foo" from myPizza object?


Solution

  • pizza.prototype.foo.call(myPizza);   // outputs "circle foo"