I am puzzled and trying to understand this...
In OO languages like C++ and Java, a member function of an object can refer to a member variable directly.
However in the object below, if a member function refers to property 'balance' directly,
balance += amount;
I get error
ReferenceError: balance is not defined
One has to qualify fully, savingsAccount.balance
Why is that so?
var savingsAccount = {
balance: 1000,
interestRatePercent: 1,
deposit: function addMoney(amount) {
if (amount > 0) {
balance += amount; <<--==-ERROR---====--ERROR---=====--- ERROR!
}
},
withdraw: function removeMoney(amount) {
var verifyBalance = savingsAccount.balance - amount;
if (amount > 0 && verifyBalance >= 0) {
savingsAccount.balance -= amount;
}
},
printAccountSummary: function print() {
return 'Welcome!\nYour balance is currently $' + savingsAccount.balance +' and your interest rate is '+ savingsAccount.interestRatePercent + '%.';
}
};
console.log(savingsAccount.printAccountSummary());