Javascript class definition

There are various techniques for creating classes in Javascript but I find the below the most basic (explicit prototype use helps me, others though equally valid, seem like syntactic sugar. Anyways my 2 cents)

//class definition 
var BankAccount = (function () {
  function BankAccount(initially) {
    this.balance = initially;
  }
  BankAccount.prototype.deposit = function (credit) {
    this.balance += credit;
    return this.balance;
  };
  return BankAccount;
})();

//instantiate object using the above class definition
var myAccount=new BankAccount(100);
console.log(myAccount.balance);
myAccount.deposit(10);
console.log(myAccount.balance);

Advertisements