Skip to main content

Posts

Showing posts from February, 2015

JavaScript Classes

ECMAScript 6 comes with the ability to use classes. JavaScript being a prototype based, classless object oriented programming language, doesn't have the notions of classes. Then how can they assimilate them? First, let's have a look at how one would traditionally use object creation and inheritance in JavaScript:

  //function imitating constructor function CustomObject(value){ //instance property; each occurrence of CustomObject will have it's own with a unique value this.property = value; } //prototype method; each occurrence of CustomObject will have access to this method CustomObject.prototype.method = function(){ return this.property; } //create new object var obj = new CustomObject("my value"); //function imitating subclass constructor function SubClassObject(value){ //similar to calling super in class based languages CustomObject.call(this, value); } //set the prototy…