OOP stands for Objected Oriented Programming.
The main principles are:
Function as a constructor:
var cat = {};
cat.weight = 3;
cat.age = 1;
Object literal:
var cat = new Cat(3, 1);
function Cat(weight, age) {
this.weight = weight;
this.age = age;
}
The ability to redefine methods for derived classes.
In JavaScript, we don't have the concept of class, so inheritance in JavaScript is prototype based. This means to implement inheritance in JavaScript, an object inherits from another object.
Advantage of using the prototype object to add functions:
var cat = new Cat(3, 1);
function Cat(weight, age) {
this.weight = weight;
this.age = age;
this.eat = function () {
alert("I want more!");
return this;
}
}
cat.eat().eat().eat();