如何在构造函数的方法中使用方法

how to use method inside method of a constructor?

本文关键字:方法 使用方法 构造函数      更新时间:2023-09-26
method.getTotalDays = function(){
    return (this.longi-this.age)*365;
}
method.eatPercent = function(){
    return this.eat/24;
}

在此构造函数中的下一个方法中,我想计算"进食过程"在我生命中花费的天数。例如,我想要一个这样的方法:

method.getEatingDays = function(){
var days = 0;
days = eatPercent*totalDays; //How do I get eatPercent and totalDays by using the established  
                             //methods?
}

如果将method定义为对象,则可以执行

days = method.eatPercent() * method.totalDays();

如果方法是一个函数,那么你需要

days = this.eatPercent() * this.totalDays();

这里的this是指调用getEatingDays()的所有者

您需要在当前实例中调用这些 getter 函数,这可以通过 this.fnName()

method.getEatingDays = function(){
var days = 0;
days = this.eatPercent()*this.getTotalDays();
}