Design Patterns
In this tutorial we will learn to create classes and objects in JavaScript that will help in design patterns.
JavaScript got the class
keyword in ES2015. Till then we were using the function
keyword to create classes.
The class
keyword is primarily a syntactical sugar over existing prototype-based inheritance.
To instantiate an object we use the new
keyword.
Since JavaScript before ES2015 (ES6) was without the class keyword so, to create classes we used the function
keyword. The this
keyword helped in setting properties and method of the object.
In the following example we are creating a Box class using the function
keyword.
// the Box class
function Box(length, breadth, height) {
// properties
this.length = length;
this.breadth = breadth;
this.height = height;
// methods
this.getVolume = function() {
return this.length * this.breadth * this.height;
};
}
// creating object of the Box class
var box = new Box(10, 20, 30);
console.log(box.getVolume()); // 6000
Following are the points to keep in mind when creating classes using the function
keyword in JavaScript.
new
keyword.this
keyword.We can also add the method getVolume
to the constructor's prototype
like the following.
// the Box class
function Box(length, breadth, height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
// method added to the Box's prototype
Box.prototype.getVolume = function() {
return this.length * this.breadth * this.height;
};
// creating object of the Box class
var box = new Box(10, 20, 30);
console.log(box.getVolume()); // 6000
With ES6 we get the class
keyword, a syntactical sugar, to create classes in JavaScript.
In the following example we are creating Box class using the class
keyword.
// the Box class
class Box {
// constructor to set initialise the object
constructor(length, breadth, height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
getVolume() {
return this.length * this.breadth * this.height;
}
}
// creating object of the Box class
var box = new Box(10, 20, 30);
console.log(box.getVolume()); // 6000
Following are the points to keep in mind when creating classes using the class
keyword in JavaScript.
new
keyword.constructor
keyword to initialise object.this
keyword to access properties and methods inside the class.In the following example we are going to create Cube which will inherit the Box.
// the Box class
function Box(length, breadth, height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
// method added to the Box's prototype
Box.prototype.getVolume = function() {
return this.length * this.breadth * this.height;
};
// the Cube class that inherits the Box
function Cube(side) {
// calling the Box's constructor
Box.call(this, side, side, side);
}
// inheriting the methods from Box's prototype
Cube.prototype = Object.create(Box.prototype);
// creating object of the Cube class
var cube = new Cube(10);
console.log(cube.getVolume()); // 1000
In the above code the Cube class inherits the properties and methods of the Box class.
We use the call
method to call the Box constructor from the context of Cube.
We use the Cube.prototype = Object.create(Box.prototype);
to inherit the method defined on Box's prototype.
In the following example we are using the extends
keyword to inherit.
// the Box class
class Box {
// constructor to set initialise the object
constructor(length, breadth, height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
getVolume() {
return this.length * this.breadth * this.height;
}
}
// the Cube class inherits the Box
class Cube extends Box {
constructor(side) {
// calling the Box's constructor
super(side, side, side);
this.side = side;
}
}
// creating object of the Cube class
var cube = new Cube(10);
console.log(cube.getVolume()); // 1000
With ES6 we have the extends
keyword to inherit classes in JavaScript.
ADVERTISEMENT