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

时间:2013-08-21 03:30:58

标签: javascript

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?
}

2 个答案:

答案 0 :(得分:2)

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

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

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

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

this此处指的是调用getEatingDays()

的所有者

答案 1 :(得分:1)

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

完成
method.getEatingDays = function(){
var days = 0;
days = this.eatPercent()*this.getTotalDays();
}
相关问题