【问题标题】:JAvascript "has no method"JAvascript“没有方法”
【发布时间】:2016-06-01 20:11:55
【问题描述】:

当它转到第 21 行时,它认为没有函数存款 我很困惑,因为它应该有这个功能。请解释原因。

1   function makeBankAccount(){
2     var bala = 0
3     function balance(b)
4     {
5       return bala;
6     }
7     function withdraw(b)
8     {
9       bala = bala - b;
10    }
11    function deposit(b)
12    {
13      bala = bala + b;
14    }
15    return makeBankAccount;
16  }
17  
18  var account1 = makeBankAccount();
19  var account2 = makeBankAccount();
20  
21  account1.deposit(5); 
22  console.log(account1.balance()); // -> 5
23  account1.withdraw(5);
24  console.log(account1.balance()); // -> 0
25  
26  account2.withdraw(5);
27  account2.withdraw(5);
28  account2.deposit(5); 
29  account2.deposit(5); 
30  account2.deposit(5);
31  account2.deposit(5);
32  console.log(account2.balance()); // -> 10

【问题讨论】:

标签: javascript


【解决方案1】:

有两种方法可以做到这一点:

function makeBankAccount(){
    var bala = 0

    function balance(b) {
        return bala;
    }

    function withdraw(b) {
        bala = bala - b;
    }

    function deposit(b) {
        bala = bala + b;
    }
    return {
        balance: balance,
        withdraw: withdraw,
        deposit: deposit
    };
}

那么你的使用代码不变

function makeBankAccount(){
    this.bala = 0
}

makeBankAccount.prototype.balance = function(b) {
    return this.bala;
};

makeBankAccount.prototype.withdraw = function(b) {
    this.bala -= b;
};

makeBankAccount.prototype.deposit = function (b) {
    this.bala += b;
};

然后你的实例化 account1/account2 喜欢

var account1 = new makeBankAccount();
var account2 = new makeBankAccount();

其余代码保持不变

【讨论】:

    【解决方案2】:

    试试这个:

    var balance = 10; //You may have the balance saved anywhere else. Get it here.
    var makeBankAccount = {
        amount: 0,
        balance: function () {
            return balance;
        },
        withdraw: function () {
            return balance - this.amount;
        },
        deposit: function () {
            return balance + this.amount;
        }
    }
    function Balance() {
      document.getElementById("Balance").innerHTML = makeBankAccount.balance();
    }
    function Withdraw(x) {
      makeBankAccount.amount = x;
      document.getElementById("Withdraw").innerHTML = makeBankAccount.withdraw();
    }
    function Deposit(x) {
      makeBankAccount.amount = x;
      document.getElementById("Deposit").innerHTML = makeBankAccount.deposit();
    }
    <html>
    <body>
      <button onclick="Balance()">Balance</button>
      <button onclick="Withdraw(6)">Withdraw</button>
      <button onclick="Deposit(7)">Deposit</button>
      <p id="Balance"></p>
      <p id="Withdraw"></p>
      <p id="Deposit"></p>
    </body>
    </html>

    剩下的事情由你自己来获取balance 并将amount 传递给函数。

    【讨论】:

      【解决方案3】:

      这是因为您的函数 deposit(b) 不是全局的。

      deposit(b) 只能在 make makeBankAccount() 内部访问,并且您的对象存在于外部。所以deposit(b) 超出范围。

      在对象创建中也缺少new 关键字。 将其更改为:

      对象 1:

      var account1 = new makeBankAccount();
      

      对象 2:

      var account2 = new makeBankAccount();
      

      【讨论】:

      • 但是 account2 不是 makebankaccount 的一个实例吗?
      • 不,它不是...没有新的关键字,并且 makebankaccount 的原型上没有(新)方法
      猜你喜欢
      • 2013-08-17
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 2022-11-18
      相关资源
      最近更新 更多