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

时间:2016-07-30 13:22:39

标签: 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);

3 个答案:

答案 0 :(得分:2)

提示和技巧

让我们从基础开始:

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

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) {

}

工作脚本

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

&#13;
&#13;
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`;
&#13;
<div id=output></div>
&#13;
&#13;
&#13;

的改进

然后,您可以进一步改进脚本,添加交易记录,甚至可以管理不同的银行

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

希望它有所帮助。

答案 1 :(得分:1)

server { server_name your_domain_without_www; location / { rewrite ^(.*)$ http://your_domain_with_www$1 permanent; } } server { server_name *.your_domain_without_www; ... } 内部,您应该account替换account

this

创建var account = function (name, balance){ this.name = name; this.balance = balance; this.deposit = function (depositAmount) { ... } } 对象后,您可以调用account来检索有关对象的信息。

答案 2 :(得分:0)

查看此JSBin:http://jsbin.com/jiheqotaye/edit?js,console

Fez所说的一切正是你在编写一个易于理解的构造函数时需要考虑的。

使用transfer方法;您想要检查传入的account是否是同一构造函数的实例。然后检查withdraw是否成功,然后继续存款。

Account.prototype.transfer = function (transferAmount, account){
  if (account instanceof Account) {
    if (this.withdraw(transferAmount)) {
      account.deposit(transferAmount);
      return true;
    } else {
      console.log("An error occured withdrawing that amount");
      return false;
    }
  }
  console.log("Provided account was not an account with our bank");
  return false;
};

希望有所帮助!