-1

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());
likejudo
  • 3,396
  • 6
  • 52
  • 107
  • 2
    `balance` is a property of the object, not a standalone variable – CertainPerformance Jul 21 '19 at 01:52
  • I don't understand what you mean. I am accessing it from `addMoney`, also a property. – likejudo Jul 21 '19 at 01:53
  • 1
    Yes, and to access it you have to treat it as a property, which is what it is. Hence the fully-qualified reference. – Robert Harvey Jul 21 '19 at 01:54
  • this is very different from other OO languages. I don't understand this behavior. – likejudo Jul 21 '19 at 01:55
  • Not so different. `Balance` has been declared inside `savingsAccount,` so it is locally-scoped to that object. `Balance` doesn't exist outside `savingsAccount`. – Robert Harvey Jul 21 '19 at 01:56
  • 1
    Slightly off topic but if you're more comfortable with OOP focused languages, I would highly recommend using javascript [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) – Ted Brownlow Jul 21 '19 at 01:56
  • Note that `deposit` has its own scope. Yes, Javascript is a weird language. Always has been. – Robert Harvey Jul 21 '19 at 01:57
  • @RobertHarvey but my point is that addMoney is also declared inside savingsAccount and since these properties are both member variables or properties, they should be able to access each other directly. I dont know why my question is downvoted. – likejudo Jul 21 '19 at 01:58
  • https://stackoverflow.com/questions/4616202/self-references-in-object-literals-initializers – epascarello Jul 21 '19 at 02:01

1 Answers1

1

Implicit member variables are shorthand for this.member (or this->member in C++). Javascript does not have implicit member variables, so you must always prefix member variables with this.

class SavingsAccount {
    constructor() {
        this.balance=1000;
        this.interestRatePercent=1;
    }
    deposit(amount) {
        if (amount > 0) {
            this.balance += amount;
        }
    }
    withdraw(amount) {
        var verifyBalance = this.balance - amount;
        if (amount > 0 && verifyBalance >= 0) {
            this.balance -= amount;
        }
    }
    printAccountSummary() {
        return 'Welcome!\nYour balance is currently $' + this.balance +' and your interest rate is '+ this.interestRatePercent + '%.';
    }
}
savingsAccount = new SavingsAccount();
savingsAccount.deposit(100);
console.log(savingsAccount.printAccountSummary());
savingsAccount.withdraw(200);
console.log(savingsAccount.printAccountSummary());
Ted Brownlow
  • 1,103
  • 9
  • 15