⬅ Go back to: Homepage

Object-Oriented Programming in JavaScript


What is an OOP?

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.


What are the 4 Pillars of Object-Oriented Programming?


What are the Benefits of OOP?


Procedural Code Sample:


    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.


Object-Oriented Programming


    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