我想创建一个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);
}
}
}
最终,有几个问:
答案 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:强>
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;
ES5原型:
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;