retireAge方法如何在同一Person函数构造函数中访问calculateAge方法的值?
答案 0 :(得分:0)
您可以在构造函数内的retirementAge
方法中简单地调用它
this.retirementAge = function() {
console.log(66 - this.calculateAge())
}
并像使用它
john.retirementAge();
答案 1 :(得分:0)
如果使您的calculateAge方法返回一个值,则可以在retirementAge方法中访问它。 试试这个:
this.calculateAge = function () {
return 2018 - this.yearOfBirth
}
this.retirementAge = function () {
return 66 - this.calculateAge()
}
}
答案 2 :(得分: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());