It combines a group of related variables and functions into a unit.
We call that unit, an object.
variables
referred as properties
while functions
as methods
.
It's a programming paradigm, a very popular style of programming.
An object with group of variables and functions that are related to each other.
Simpler Interface. Reduce the Impact of Change.
It is a mechanism that allows you to eliminate redundant code.
"Poly" means many, "morphism" means form. It's a technique that allows you to get rid of long if
and else
, switch
and case
statements.
We can implement a render method in each objects, then the render method will behave differently depending on the type of the object we are referencing.
We can reduce complexity and increase reusability.
We can hide the details and the complexity then show only the essentials. We can also isolate the impact of changes.
We can eliminate redundant code.
We can refactor the ugly switch
and case
statements.
let baseSalary = 30_000;
let overtime = 10;
let rate = 20;
function getWage(baseSalary, overtime, rate) {
return baseSalary + (overtime * rate);
}
This kind of implementation is called procedural. We have variables on one side and functions on the other side.
let employee = {
baseSalary: 30_000,
overtime: 10,
rate: 20,
getWage: function() {
return this.baseSalary + (this.overtime this.rate);
}
};
employee.getWage();
The fewer the number of parameters, the easier it is to use and maintain that function.
The best functions are those with no parameters! - Robert C Martin