OOP
Object-Oriented Programming
What is OOP?
- OOP is a programming paradigm based on the concept of objects
- We use objects to model(describe) real-world or abstract features
- Objects may contain data (properties) and code (methods). By using objects, we pack data and the corresponding behavior into one block.
- In OOP, objects are self-contained pieces/blocks of code
- Objects are building blocks of applications, and interact with one another
- Interactions happen through a public interface(API): methods that the code out side of the object can access and use to communicate with the object;
Four Fundamental principles of OOP
- abstraction
- encapsulation
- inheritance
- polumorphism
OOP In JS

Prototypes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| console.log(Person.prototype);
Person.prototype.calcAge = function () { console.log(2037 - this.birthYear); };
jonas.calcAge(); matilda.calcAge();
console.log(jonas.__proto__); console.log(jonas.__proto__ === Person.prototype);
console.log(Person.prototype.isPrototypeOf(jonas)); console.log(Person.prototype.isPrototypeOf(matilda)); console.log(Person.prototype.isPrototypeOf(Person));
Person.prototype.species = 'Homo Sapiens'; console.log(jonas.species, matilda.species);
console.log(jonas.hasOwnProperty('firstName')); console.log(jonas.hasOwnProperty('species'));
|


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| console.log(jonas.__proto__);
console.log(jonas.__proto__.__proto__); console.log(jonas.__proto__.__proto__.__proto__);
console.dir(Person.prototype.constructor);
const arr = [3, 6, 6, 5, 6, 9, 9]; console.log(arr.__proto__); console.log(arr.__proto__ === Array.prototype);
console.log(arr.__proto__.__proto__);
Array.prototype.unique = function () { return [...new Set(this)]; };
console.log(arr.unique());
const h1 = document.querySelector('h1'); console.dir(x => x + 1);
|
ES6 Classes
- Classes are NOT hoisted
- Classes are first-class citizens
- Classes are executed in strict mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
class PersonCl { constructor(fullName, birthYear) { this.fullName = fullName; this.birthYear = birthYear; }
calcAge() { console.log(2037 - this.birthYear); }
greet() { console.log(`Hey ${this.fullName}`); }
get age() { return 2037 - this.birthYear; }
set fullName(name) { if (name.includes(' ')) this._fullName = name; else alert(`${name} is not a full name!`); }
get fullName() { return this._fullName; }
static hey() { console.log('Hey there 👋'); console.log(this); } }
const jessica = new PersonCl('Jessica Davis', 1996); console.log(jessica); jessica.calcAge(); console.log(jessica.age);
console.log(jessica.__proto__ === PersonCl.prototype);
jessica.greet();
|
Setters and Getters
Static Methods and Instance Methods
Object.create()

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| const PersonProto = { calcAge() { console.log(2037 - this.birthYear); },
init(firstName, birthYear) { this.firstName = firstName; this.birthYear = birthYear; }, };
const steven = Object.create(PersonProto); console.log(steven); steven.name = 'Steven'; steven.birthYear = 2002; steven.calcAge();
console.log(steven.__proto__ === PersonProto);
const sarah = Object.create(PersonProto); sarah.init('Sarah', 1979); sarah.calcAge();
|
Inheritance between Classes
