javascript - 创建一个基本的银行系统,javascript中的关键字 'this'

标签 javascript this

我的任务是创建一个包含多个帐户的银行。银行需要一种方法来返回账户中的总金额。它还需要一个 addAccount 方法,该方法将在银行注册一个新帐户并将其添加到帐户数组中。您应该能够向帐户存款或取款以更改余额。确保帐户不能有负值。在银行上写下“转账”,允许您在两个帐户之间转账。

我的问题是找到一种方法来打印每个用户的详细信息(姓名、余额)。我觉得我通过应用关键字 this 错过了某种链接。 这是我的代码:

var account = function (name, balance){

  account.name = name;
  account.balance = balance;

  account.deposit = function (depositAmount) {
    newBalance = account.balance - depositAmount;
    console.log("Your balance is now " + newBalance);
    if (newBalance <= 0) {
      console.log("You have insufficient funds!!!");
    }
  };

  account.withdraw = function (withdrawAmount){
    newBalance = account.balance - withdrawAmount;
    console.log("Your balance is now " + newBalance);
    if (newBalance <= 0) {
      console.log("You have insufficient funds!!!");
    }

  };

  account.transfer = function (transferAmount){
//got stuck here
  }

  console.log("Name: " + name + "; Balance: " + balance);
}


var AustinAccount = new account ("Austin", 500);
var KateAccount = new account ("Kate", 10000);
var GingerAccount = new account ("Ginger", 70000000);
var OreoAccount = new account ("Oreo", 900000000);

最佳答案

提示和技巧

让我们从基础开始:

构造函数应以大写字母开头:

function Account(name, balance) {

}

然后,正如 @Ibar 所说,您应该使用 this 来引用构造函数实例,而不是通过名称调用它。

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

然后,您可以通过以下方式访问任何实例属性:

var account = new Account('Hey', 100);
console.log(account.name);

此外,定义构造函数方法的正确方法是:

function Account(name, balance) {

}

Account.prototype.deposit = function(arg) {

}
<小时/>

工作脚本

在这里您可以找到一个工作脚本,我添加了两个私有(private)方法(_isPositive_isAllowed),用于检查给定数量是否为正数,以及是否有足够的数量给定交易后帐户中的资金。

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

Account.prototype.deposit = function(amount) {
  if (this._isPositive(amount)) {
    this.balance += amount;
    console.info(`Deposit: ${this.name} new balance is ${this.balance}`);
    return true;
  }
  return false;
}

Account.prototype.withdraw = function(amount) {
  if (this._isAllowed(amount)) {
    this.balance -= amount;
    console.info(`Withdraw: ${this.name} new balance is ${this.balance}`);
    return true;
  }
  return false;
}

Account.prototype.transfer = function(amount, account) {
  if (this.withdraw(amount) && account.deposit(amount)) {
    console.info(`Transfer: ${amount} has been moved from ${this.name} to ${account.name}`);
    return true;
  }
  return false;
}


Account.prototype._isPositive = function(amount) {
  const isPositive = amount > 0;
  if (!isPositive) {
    console.error('Amount must be positive!');
    return false;
  }
  return true;
}

Account.prototype._isAllowed = function(amount) {
  if (!this._isPositive(amount)) return false;

  const isAllowed = this.balance - amount >= 0;
  if (!isAllowed) {
    console.error('You have insufficent funds!');
    return false;
  }
  return true;
}

const a = new Account('a', 100);
const b = new Account('b', 0);


output.innerText += `before:  a: ${a.balance}, b: ${b.balance}\n`;

a.transfer(100, b);

output.innerText += `after:  a: ${a.balance}, b: ${b.balance}\n`;
<div id=output></div>

<小时/>

改进

然后,您可以进一步改进脚本,添加交易日志,甚至可能是管理不同银行的方法

const fooBank = new Bank('foo');
const fezAccount = new fooBank.Account('fez', 0);

希望有帮助。

关于javascript - 创建一个基本的银行系统,javascript中的关键字 'this',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38674405/

相关文章:

javascript - 获取单个/多个文本框值

c# - 在参数 c# 中使用 this 关键字

javascript - 如何指示从对象调用的绑定(bind)成员函数的上下文类型?

Jquery Slidedown第一个子级元素

javascript - 如何在 JavaScript 2015 (EcmaScript 6) 中给它取别名?

javascript - 仅当焦点位于文本框中时发出警报

Javascript:如何防止页面加载时播放音频?

javascript - 加载嵌入在mouseenter和静音上的youtube

javascript - 插件附加脚本与 jpm run 很好,但与实际的 xpi 不一样

javascript - 使用另一个属性计算对象属性值