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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s