在同一个 Person 函数构造函数中,retireAge 方法如何访问 calculateAge 方法的值?

3 回答
0
您可以简单地retirementAge在构造函数内部的方法中调用它
this.retirementAge = function() {
console.log(66 - this.calculateAge())
}
并像使用它一样
john.retirementAge();
于 2018-09-25T07:50:21.053 回答
0
如果你让你的calculateAge 方法返回一个值,你将能够在retireageAge 方法中访问它。尝试这个:
this.calculateAge = function () {
return 2018 - this.yearOfBirth
}
this.retirementAge = function () {
return 66 - this.calculateAge()
}
}
于 2018-09-25T07:57:27.343 回答
0
您可以制作方法来重新调整值,然后this.calculateAge()在获取值的方法中使用。
var Person = function(name, yearOfBirth, job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
this.calculateAge = function() {
return 2018 - this.yearOfBirth;
};
this.retirementAge = function() {
return 66 - this.calculateAge();
}
};
var john = new Person("John", 1998, 'teacher');
console.log(john.retirementAge());
console.log(john.calculateAge());
于 2018-09-25T08:11:15.533 回答