OOP vs functional programming OOP - object oriented programming It is just the principle of code organization inside objects Functional programming, encapsulation in object, class
const baseSalary = 30_000
const overtime = 10
const rate = 20
function getWage(baseSalary, overtime, rate) {
return baseSalary + overtime * rate;
}
getWage(baseSalary, overtime, rate);
Encapsulation inside object
const employee = {
baseSalary: 30_000,
overtime: 10,
rate: 20,
getWage: function () {
return this.baseSalary + this.overtime * this.rate
}
}
employee.getWage()
Same object created via class
class Employee {
baseSalary = 30_000
overtime = 10
rate = 20
getWage () {
return this.baseSalary + this.overtime * this.rate
}
}
const employee = new Employee()
employee. getWage()
OOP has 4 key principles: encapsulation, inheritance, polymorphism, abstraction Encapsulation Combining relative functions and variables in a single unit(object).
class Employee {
setEmpDetails(name, id) {
this.name = name
this.id = id
}
getEmpName() {
return this.name
}
getEmpId() {
return this.id
}
}
const emp1 = new Employee()
emp1.setEmpDetails('John', 1001)
emp1.getEmpName() // 'John'
emp1.getEmpId() // 1001
Inheritance Process where one object (class) gets properties from another object Inheritance is done from a class, which is called parent or super or base Inherited properties go to a class, which is called child or sub or derived
class Car {
setName(name) {
this.name = name
}
startEngine() {
console.log('Engine started for ' + this.name)
}
}
class Toyota extends Car {
topSpeed(speed) {
console.log('Top speed for ' + this.name + ' is ' + speed)
}
}
const myCar = new Toyota()
myCar.setName('Camry')
myCar.startEngine() // Engine started for Camry
myCar.topSpeed(200) // Top speed for Camry is 200
Polymorphism A way to create a single variable, function or object in different forms. Same method works differently depending on class.
class Animal {
constructor(name) {
this.name = name
}
eats() {
console.log(this.name + ' eats food')
}
}
class Alligator extends Animal {
eats() {
console.log(this.name + ' eats fishes')
}
}
const bobby = new Animal('Bobby')
bobby.eats() // Bobby eats food
const murphy = new Alligator('Murphy')
murphy.eats() // Murphy eats fishes
Abstraction Abstraction is when we hide the complexity of the code, and also not letting the user access some data Can do that by using let , const keywords & # private methods Can be easily done in function constructors, but not in class In class we can use let & const only inside functions, but it is not always elegant If we use this keyword that property will be exposed to a user
class Employee {
constructor(name, yearsInCompany) {
this.name = name
this.yearsInCompany = yearsInCompany
}
#calcSalarySecretly() {
const hiddenBaseSalary = 2000
return (1 + this.yearsInCompany / 10) * hiddenBaseSalary
}
get salary() {
return this.#calcSalarySecretly()
}
tellSalary() {
console.log('Salary of ' + this.name + ' is ' + this.salary + ' $/m')
}
}
const john = new Employee('John', 5)
john.salary // 3000
john.tellSalary() // 'Salary of John is 3000 $/m'
john.#calcSalarySecretly() // SyntaxError: Private field
john.hiddenBaseSalary // undefined