调用基本原型函数

时间:2013-01-08 08:02:18

标签: javascript inheritance prototypejs polymorphism

我有以下两个对象:

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);

当我执行以下操作时

var foo = myPizza.foo();

按预期打印以下内容:

  

pizza foo

如何激活基类并打印" circle foo"来自myPizza对象?

1 个答案:

答案 0 :(得分:2)

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