gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:20:51 26 4
gpt4 key购买 nike

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

最佳答案

如果方法被定义为一个对象,你可以这样做

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

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

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

this 这里指的是调用 getEatingDays()

的所有者

关于javascript - 如何在构造函数的方法内部使用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18348418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com