0

I understand that these two calls should return the same value. I think the assignment I made is wrong but I don't see how to do it right.

info2 () returns the correct value while info () does not.

Client.prototype.info2 = function () {
    return (this.AccountType())
}
Client.prototype.info = function () {
    const {AccountType} = this
    return (AccountType())
}

------- All code -------

function Client(name, balance) {
    this.name = name;
    this.balance = balance;
}

Client.prototype.AccountType = function () {
    const {balance} = this
    let type;

    if (balance >= 10000) {
        type = 'Platinum'
    } else if (balance >= 5000) {
        type = 'Gold'
    } else {
        type = 'Standard'
    }
    return type;
}

//Expected result: 'Gold'
// Result: 'Gold'

Client.prototype.info = function () {
    const {AccountType} = this
    return (AccountType())
}
//Expected result: 'Gold'
// Result: 'Standard'

Client.prototype.info2 = function () {
    return (this.AccountType())
}
//Expected result: 'Gold'
// Result: 'Gold'

//instances

const Pedro = new Client('Pedro', 8000)
Okicarde
  • 16
  • 2
  • 1
    No, they shouldn't. By separating the property access from the call, you've broken the link that sets `this` during the call to the expected value. See the linked questions' answers for details. – T.J. Crowder Mar 17 '22 at 09:58
  • Side note: There's no need for `()` around the operand to `return` unless you want a line break just after the `return` (in which case you do need `()` -- with the first `(` on the same line as `return` -- because otherwise ASI will bite you). – T.J. Crowder Mar 17 '22 at 10:00

0 Answers0