在ES6类中出现静态字段(非方法,原始)的问题?

时间:2017-05-30 06:44:06

标签: javascript ecmascript-6 es6-class

我想创建一个BankAccount类,其中新实例跟踪新实例的检查和节省。但是,每当有人创建一个新实例时,我想要一个静态方法,它调用一个仅与BankAccount类相关联的静态字段,它跟踪有多少银行账户是开放的(有点像一个行为)单)。这在c#/ java中是可行的,但很难在Javascript中重新创建预期的行为。

我也在ES5中尝试了这个,但是很简单,我最接近的行为是我正在寻找使用函数作为封装的封装器:

function BankAccountMaker() {
    var countOfAccounts = 0;
    function IncrementcountOfAccounts() {
        countOfAccounts += 1;
    }

    function BankAccount(c = 0) {
        this.checking = 0;
    }

    return {
        makeBankAccount: function (c) {
            IncrementcountOfAccounts();
            console.log(countOfAccounts)
            return new BankAccount(c);
        }
    }
}

最终,有几个问:

  1. 有人可以编写与上述相同的ES6代码吗?
  2. 另外,有没有更好的方法在ES5中执行此操作,我不需要包装器?
  3. 为什么在ES5中我们可以将原型方法设置为原语,但似乎我们不能用ES6语法来做到这一点? (我使用的是Node 6.1和Babel fwiw)

1 个答案:

答案 0 :(得分:2)

我已为每个银行帐户添加了ID。因此,您可以遍历创建的帐户。 incrementcountAccounts是一个坏主意,因为您不知道银行帐户是否真的被创建。

您无法执行此操作:ES5不支持BankAccount(c = 0)个参数。

一些片段:



/**
 * Bank
 */
class Bank {
  constructor(name, id) {
    this.countOfAccounts = 0;
    this.id = id;
    this.name = name;
    
    console.log('--- ' + this.name + ' ---');
  }

  incrementcountOfAccounts() {
    this.countOfAccounts += 1;
  }
  
  addBankAccount(...data) {
    this.incrementcountOfAccounts();
    
    return new BankAccount(this.id, ...data);
  }
}

/**
 * BankAccount
 */
class BankAccount {
  /**
   * Constrctor
   * @param  {Number} bankId     
   * @param  {Number} [amount=0]   
   * @param  {Number} [checking=0]            
   */
  constructor(bankId, amount = 0, checking = 0) {
    if(!bankId) {
      throw Error('no bank id');
    }
    
    this.account = {
      bankId,
      checking, 
      amount
    };
  }
  
  /**
   * Get bank id
   * @return {number}
   */
  get bankId() {
    return this.account.bankId;
  }

  /**
   * Set amount
   * @param  {number} value
   * @return {number}       
   */
  set amount(value) {
    this.account.amount = value;
  }  

  /**
   * Get ammunt
   * @return {number}
   */
  get amount() {
    return this.account.amount;
  }

  /**
   * set checking
   * @param  {number} value
   * @return {number}     
   */
  set checking(value) {
    this.account.checking = value;
  }  

  /**
   * get checking
   * @return {number} 
   */
  get checking() {
    return this.account.checking;
  }
}

const FirstBank = new Bank('First Bank', 1);


const bankAccount1 = FirstBank.addBankAccount(300);
const bankAccount2 = FirstBank.addBankAccount(4000);


console.log('Account 1: ' + bankAccount1.amount + ' - ' + bankAccount1.checking + ' - ' + bankAccount1.bankId);
console.log('Account 2: ' + bankAccount2.amount + ' - ' + bankAccount1.checking + ' - ' + bankAccount1.bankId);


console.log('Accounts first Bank: ' + FirstBank.countOfAccounts);

const SecondBank = new Bank('Second Bank', 4);
const bankAccount3 = SecondBank.addBankAccount(0, 1);
console.log('Account 3: ' + bankAccount3.amount + ' - ' + bankAccount1.checking + ' - ' + bankAccount1.bankId);    
console.log('Accounts second Bank: ' + SecondBank.countOfAccounts);




<强> ES5:

&#13;
&#13;
var BankAccountMaker = function () {
  this.countOfAccounts = 0;
  
  this.IncrementcountOfAccounts = function () {
    this.countOfAccounts += 1;
  }
  
  this.BankAccount = function () {
    this.checking = 0;
  }
  
  this.makeBankAccount = function (c) {
    this.IncrementcountOfAccounts();

    return new this.BankAccount(c);
  }
}

const Bank = new BankAccountMaker();

console.log(Bank.makeBankAccount(3));
console.log(Bank.makeBankAccount(3));
console.log(Bank.makeBankAccount(3));
console.log(Bank.countOfAccounts);
&#13;
&#13;
&#13;

ES5原型:

&#13;
&#13;
function BankAccountMaker() {
  this.countOfAccounts = 0;

}

BankAccountMaker.prototype.IncrementcountOfAccounts = function() {
  this.countOfAccounts += 1;
}

BankAccountMaker.prototype.BankAccount = function() {
  this.checking = 0;
}

BankAccountMaker.prototype.IncrementcountOfAccounts = function() {
  this.countOfAccounts += 1;
}

BankAccountMaker.prototype.makeBankAccount = function(c) {
  this.IncrementcountOfAccounts();
  console.log(this.countOfAccounts);
  
  return new this.BankAccount(c);
}

const Bank = new BankAccountMaker();

console.log(Bank.makeBankAccount(3));
console.log(Bank.makeBankAccount(3));
console.log(Bank.makeBankAccount(3));
console.log(Bank.countOfAccounts);
&#13;
&#13;
&#13;