簡體   English   中英

對象值未更新且不知道為什么

[英]Object Values Not Updating and Don't Know Why

我有一個構造函數來創建帳戶並管理其數據,如下所示:

        function account(fname, lname){
            this.checking = 0;
            this.savings = 0;
            this.total = this.checking + this.savings;
            this.openDate = new Date();
            this.transactions = [];
            this.firstName = fname;
            this.lastName = lname;
            this.deposit = (account, amount) => {
                account += amount;
            };
            this.withdrawal = (account, amount) => {
                account -= amount;
            };
        }

...但我遇到的問題是,每當我嘗試使用存款或取款功能時,相關值都不會更新。 我這樣寫:

var client = new account("Name", "Name");
client.deposit(client.checking, 5000);

......仍然沒有骰子。 有什么建議?

編輯:

我一直有 IDE 問題,但我們需要首先將其視為問題(使用 NetBeans)。

字符串和數字是 Javascript 中的不可變屬性,不會通過引用進行修改。 您應該改為修改實際的對象實例值

 function account(fname, lname){ this.checking = 0; this.savings = 0; this.total = this.checking + this.savings; this.openDate = new Date(); this.transactions = []; this.firstName = fname; this.lastName = lname; this.deposit = (account, amount) => { this[account] += amount; }; this.withdrawal = (account, amount) => { this[account] -= amount; } } var client = new account("Name", "Name"); client.deposit('checking', 5000); console.log(client);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM