I encountered a challenge while trying to access the value returned in one method within a property of another method belonging to a different constructor. The specific error message I received was "TypeError: Cannot read property 'name' of undefined."
class Loans {
constructor(user, loanAmount, tenor, id = 0, status = 'pending', repaid = 'false') {
this.id = id + 1;
this.user = user;
this.loanAmount = loanAmount;
this.tenor = tenor;
this.status = status;
this.repaid = repaid;
this.interest = (function interest() {
return (loanAmount * 0.05);
}());
this.monthlyInstall = (function monthlyInstall() {
return (loanAmount + this.interest) / tenor;
}());
this.balance = (function balance() {
return (loanAmount + interest);
}());
this.createdAt = new Date().toLocaleString();
};
};
const loan = new Loans('steve.jobs', 50000, 5);
console.log(loan);
However, when attempting to execute the code, I encountered the following error message:
return (loanAmount + this.interest) / tenor;
^
TypeError: Cannot read property 'interest' of undefined
at monthlyInstall (C:\Users\DEBAYO\Desktop\JavaScript\Challenges\testing.js:183:33)
at new Loans (C:\Users\DEBAYO\Desktop\JavaScript\Challenges\testing.js:184:6)