Javascript 给家长打电话';什么方法?

Javascript 给家长打电话';什么方法?,javascript,Javascript,调用父方法。如何实现 function Ch() { this.year = function (n) { return n } } function Pant() { this.name = 'Kelli'; this.year = function (n) { return 5 + n } } //延伸 Pant.prototype

调用父方法。如何实现

 function Ch() {
        this.year = function (n) {
            return n
        }
    }

    function Pant() {
        this.name = 'Kelli';
        this.year = function (n) {
            return 5 + n
        }
    }
//延伸

 Pant.prototype = new Ch();
    Pant.prototype.constructor = Pant;
    pant = new Pant();
    alert(pant.name); //Kelli
    alert(pant.year(5)) //10
如何选择所有父方法

this.year = function (n) {
            return 5 + n
        } 

在对象中?感谢大家的帮助

您可以使用
\uuuuu proto\uuuu
调用重写的superclass(Parent)方法,但IE不支持它

alert(pant.__proto__.year(5)) //5

您可以使用
\uuuu proto\uuuu
调用重写的SuperClass(父)方法,但IE不支持该方法

alert(pant.__proto__.year(5)) //5
以下是如何实现继承:

goog.inherits = function(childCtor, parentCtor) {
  function tempCtor() {};
  tempCtor.prototype = parentCtor.prototype;
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new tempCtor();
  childCtor.prototype.constructor = childCtor;
};
然后,您的代码将类似于:

function Ch() {}
Ch.prototype.year = 
function (n) {
   return n
}

function Pant() {}
goog.inherits(Pant,Ch);
Pant.prototype.name = 'Kelli';
Pant.prototype.year = function (n) {
   return 5 + Pant.superClass_.year.call(this, n);//Call the parent class
}

pant = new Pant();
alert(pant.name); //Kelli
alert(pant.year(5)) //10
如果需要,您当然可以重命名
goog.inherits
函数。

以下是实现继承的方法:

goog.inherits = function(childCtor, parentCtor) {
  function tempCtor() {};
  tempCtor.prototype = parentCtor.prototype;
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new tempCtor();
  childCtor.prototype.constructor = childCtor;
};
然后,您的代码将类似于:

function Ch() {}
Ch.prototype.year = 
function (n) {
   return n
}

function Pant() {}
goog.inherits(Pant,Ch);
Pant.prototype.name = 'Kelli';
Pant.prototype.year = function (n) {
   return 5 + Pant.superClass_.year.call(this, n);//Call the parent class
}

pant = new Pant();
alert(pant.name); //Kelli
alert(pant.year(5)) //10
当然,您可以根据需要重命名
goog.inherits
函数。

适应您的代码:

function Ch() {
    this.year = function(n) {
        return n;
    }
}

function Pant() {
    Ch.call(this); // make this Pant also a Ch instance
    this.name = 'Kelli';
    var oldyear = this.year;
    this.year = function (n) {
        return 5 + oldyear(n);
    };
}
// Let Pant inherit from Ch
Pant.prototype = Object.create(Ch.prototype, {constructor:{value:Pant}});

var pant = new Pant();
alert(pant.name); // Kelli
alert(pant.year(5)) // 10
适应您的代码:

function Ch() {
    this.year = function(n) {
        return n;
    }
}

function Pant() {
    Ch.call(this); // make this Pant also a Ch instance
    this.name = 'Kelli';
    var oldyear = this.year;
    this.year = function (n) {
        return 5 + oldyear(n);
    };
}
// Let Pant inherit from Ch
Pant.prototype = Object.create(Ch.prototype, {constructor:{value:Pant}});

var pant = new Pant();
alert(pant.name); // Kelli
alert(pant.year(5)) // 10

首先,假设
Ch
代表“child”,而
Pant
代表“parent”,那么您是在反向操作,这非常令人困惑。当你说

Pant.prototype = new Ch();
您正在使
Pant
继承自
Ch
。我假设这就是你的意思,你想调用返回
n
的方法,而不是返回
n+5
的方法。所以你可以这样做:

function Ch() {
    this.year = function (n) {
        return n;
    }
}

function Pant() {
    this.name = 'Kelli';
    this.year = function (n) {
        return 5 + n;
    }
}

Pant.prototype = new Ch();
Pant.prototype.constructor = Pant;
pant = new Pant();
alert(pant.name); //Kelli
alert(pant.year(5)) //10

// Is the below what you need?
alert(Pant.prototype.year(5)); // 5

首先,假设
Ch
代表“child”,而
Pant
代表“parent”,那么您是在反向操作,这非常令人困惑。当你说

Pant.prototype = new Ch();
您正在使
Pant
继承自
Ch
。我假设这就是你的意思,你想调用返回
n
的方法,而不是返回
n+5
的方法。所以你可以这样做:

function Ch() {
    this.year = function (n) {
        return n;
    }
}

function Pant() {
    this.name = 'Kelli';
    this.year = function (n) {
        return 5 + n;
    }
}

Pant.prototype = new Ch();
Pant.prototype.constructor = Pant;
pant = new Pant();
alert(pant.name); //Kelli
alert(pant.year(5)) //10

// Is the below what you need?
alert(Pant.prototype.year(5)); // 5

Pant
<代码>父级?这些是一样的吗?如果是,那么
Ch
是否代表
子项
?如果是这样的话,那么您的命名就会非常混乱,因为父对象继承了子对象……无论如何,您成功地调用了
.year()
,所以我不确定您的问题是什么。看看为什么
year
方法不在
Ch
的原型上?如果有原因,请告诉我们您的实际代码;否则移动它。所有方法都必须在原型中?为什么?
Pant
<代码>父级?这些是一样的吗?如果是,那么
Ch
是否代表
子项
?如果是这样的话,那么您的命名就会非常混乱,因为父对象继承了子对象……无论如何,您成功地调用了
.year()
,所以我不确定您的问题是什么。看看为什么
year
方法不在
Ch
的原型上?如果有原因,请告诉我们您的实际代码;否则请移动它。所有方法都必须在prototype中?为什么?ie不支持此属性。ie不支持此属性。这些问题令人困惑,但我相信OP只是想调用
Pant.prototype.year
(考虑原始代码中的
prototype
,而不是
Object.create
返回的属性)@bfavaretto:是的,这让人困惑。然而,不幸的是,该方法不在原型上……问题令人困惑,但我相信OP只是想调用
Pant.prototype.year
(考虑原始代码中的
原型
,而不是
Object.create
返回的原型)。@bfavaretto:是的,这令人困惑。然而,不幸的是,该方法不在原型上。。。