How can the retirementAge method access the value of calculateAge method in the same Person function constructor?
You could simply call it within the retirementAge
method inside the constructor function
this.retirementAge = function() {
console.log(66 - this.calculateAge())
}
and use it like
john.retirementAge();
If you make your calculateAge method return a value, you will be able to access it in the retirementAge method. Try this:
this.calculateAge = function () {
return 2018 - this.yearOfBirth
}
this.retirementAge = function () {
return 66 - this.calculateAge()
}
}
You could make the methods to retun a value and then use this.calculateAge()
in the method for getting the value.
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.