7.4 — Quick Revision: OOP in JavaScript
<< Overview
OOP Pillars
| Pillar | Meaning | JS Mechanism |
|---|
| Encapsulation | Hide internals | #privateField |
| Abstraction | Expose only essentials | Methods as interface |
| Inheritance | Reuse parent behavior | extends / super |
| Polymorphism | Same method, different behavior | Method overriding |
Class syntax cheat sheet
class Animal {
#name;
static count = 0;
constructor(name) {
this.#name = name;
Animal.count++;
}
get name() { return this.#name; }
set name(n) { this.#name = n; }
speak() { return "..."; }
static getCount() { return Animal.count; }
}
class Dog extends Animal {
constructor(name, breed) {
super(name);
this.breed = breed;
}
speak() { return "Woof!"; }
}
C++ equivalent
class Animal {
private:
string name;
public:
static int count;
Animal(const string& n) : name(n) { count++; }
virtual string speak() const { return "..."; }
string getName() const { return name; }
virtual ~Animal() = default;
};
class Dog : public Animal {
public:
string breed;
Dog(const string& n, const string& b) : Animal(n), breed(b) {}
string speak() const override { return "Woof!"; }
};
this behavior
| Context | this refers to |
|---|
Method call obj.method() | obj |
| Arrow function | Enclosing scope's this |
| Regular function (strict) | undefined |
Constructor (new) | New object being created |
| Event handler | Element that fired event |
Common pitfalls
- Forgetting
super() in child constructor
- Using
this before super() call
- Losing
this in callbacks (use arrow functions)
- Confusing
static and instance methods
- Not using
# for truly private fields
Design patterns
| Pattern | Purpose | Key idea |
|---|
| Singleton | One instance | Private constructor, static getInstance |
| Factory | Create objects without specifying class | Switch on type |
| Observer | React to state changes | Subscribe/notify |
| Strategy | Interchangeable algorithms | Inject algorithm at runtime |
| Decorator | Add behavior dynamically | Wrap original object |
Self-check drill
SC-001
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-002
- Q: What is
this?
- A: Reference to the current object instance
SC-003
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-004
- Q: super() does what?
- A: Calls the parent class constructor
SC-005
- Q: Private field syntax in JS?
- A: #fieldName
SC-006
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-007
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-008
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-009
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-010
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-011
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-012
- Q: What is
this?
- A: Reference to the current object instance
SC-013
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-014
- Q: super() does what?
- A: Calls the parent class constructor
SC-015
- Q: Private field syntax in JS?
- A: #fieldName
SC-016
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-017
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-018
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-019
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-020
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-021
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-022
- Q: What is
this?
- A: Reference to the current object instance
SC-023
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-024
- Q: super() does what?
- A: Calls the parent class constructor
SC-025
- Q: Private field syntax in JS?
- A: #fieldName
SC-026
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-027
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-028
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-029
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-030
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-031
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-032
- Q: What is
this?
- A: Reference to the current object instance
SC-033
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-034
- Q: super() does what?
- A: Calls the parent class constructor
SC-035
- Q: Private field syntax in JS?
- A: #fieldName
SC-036
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-037
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-038
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-039
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-040
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-041
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-042
- Q: What is
this?
- A: Reference to the current object instance
SC-043
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-044
- Q: super() does what?
- A: Calls the parent class constructor
SC-045
- Q: Private field syntax in JS?
- A: #fieldName
SC-046
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-047
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-048
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-049
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-050
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-051
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-052
- Q: What is
this?
- A: Reference to the current object instance
SC-053
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-054
- Q: super() does what?
- A: Calls the parent class constructor
SC-055
- Q: Private field syntax in JS?
- A: #fieldName
SC-056
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-057
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-058
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-059
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-060
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-061
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-062
- Q: What is
this?
- A: Reference to the current object instance
SC-063
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-064
- Q: super() does what?
- A: Calls the parent class constructor
SC-065
- Q: Private field syntax in JS?
- A: #fieldName
SC-066
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-067
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-068
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-069
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-070
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-071
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-072
- Q: What is
this?
- A: Reference to the current object instance
SC-073
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-074
- Q: super() does what?
- A: Calls the parent class constructor
SC-075
- Q: Private field syntax in JS?
- A: #fieldName
SC-076
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-077
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-078
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-079
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-080
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-081
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-082
- Q: What is
this?
- A: Reference to the current object instance
SC-083
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-084
- Q: super() does what?
- A: Calls the parent class constructor
SC-085
- Q: Private field syntax in JS?
- A: #fieldName
SC-086
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-087
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-088
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-089
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-090
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-091
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-092
- Q: What is
this?
- A: Reference to the current object instance
SC-093
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-094
- Q: super() does what?
- A: Calls the parent class constructor
SC-095
- Q: Private field syntax in JS?
- A: #fieldName
SC-096
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-097
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-098
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-099
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-100
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-101
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-102
- Q: What is
this?
- A: Reference to the current object instance
SC-103
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-104
- Q: super() does what?
- A: Calls the parent class constructor
SC-105
- Q: Private field syntax in JS?
- A: #fieldName
SC-106
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-107
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-108
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-109
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-110
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-111
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-112
- Q: What is
this?
- A: Reference to the current object instance
SC-113
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-114
- Q: super() does what?
- A: Calls the parent class constructor
SC-115
- Q: Private field syntax in JS?
- A: #fieldName
SC-116
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-117
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-118
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-119
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-120
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-121
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-122
- Q: What is
this?
- A: Reference to the current object instance
SC-123
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-124
- Q: super() does what?
- A: Calls the parent class constructor
SC-125
- Q: Private field syntax in JS?
- A: #fieldName
SC-126
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-127
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-128
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-129
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-130
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-131
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-132
- Q: What is
this?
- A: Reference to the current object instance
SC-133
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-134
- Q: super() does what?
- A: Calls the parent class constructor
SC-135
- Q: Private field syntax in JS?
- A: #fieldName
SC-136
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-137
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-138
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-139
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-140
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-141
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-142
- Q: What is
this?
- A: Reference to the current object instance
SC-143
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-144
- Q: super() does what?
- A: Calls the parent class constructor
SC-145
- Q: Private field syntax in JS?
- A: #fieldName
SC-146
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-147
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-148
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-149
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-150
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-151
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-152
- Q: What is
this?
- A: Reference to the current object instance
SC-153
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-154
- Q: super() does what?
- A: Calls the parent class constructor
SC-155
- Q: Private field syntax in JS?
- A: #fieldName
SC-156
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-157
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-158
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-159
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-160
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-161
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-162
- Q: What is
this?
- A: Reference to the current object instance
SC-163
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-164
- Q: super() does what?
- A: Calls the parent class constructor
SC-165
- Q: Private field syntax in JS?
- A: #fieldName
SC-166
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-167
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-168
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-169
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-170
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-171
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-172
- Q: What is
this?
- A: Reference to the current object instance
SC-173
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-174
- Q: super() does what?
- A: Calls the parent class constructor
SC-175
- Q: Private field syntax in JS?
- A: #fieldName
SC-176
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-177
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-178
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-179
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-180
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-181
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-182
- Q: What is
this?
- A: Reference to the current object instance
SC-183
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-184
- Q: super() does what?
- A: Calls the parent class constructor
SC-185
- Q: Private field syntax in JS?
- A: #fieldName
SC-186
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-187
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-188
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-189
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-190
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists
SC-191
- Q: Four pillars of OOP?
- A: Encapsulation, Abstraction, Inheritance, Polymorphism
SC-192
- Q: What is
this?
- A: Reference to the current object instance
SC-193
- Q: extends keyword does what?
- A: Creates a child class that inherits from a parent
SC-194
- Q: super() does what?
- A: Calls the parent class constructor
SC-195
- Q: Private field syntax in JS?
- A: #fieldName
SC-196
- Q: Static method called how?
- A: ClassName.method(), not instance.method()
SC-197
- Q: What is polymorphism?
- A: Same method name, different behavior per class
SC-198
- Q: Composition vs inheritance?
- A: has-a vs is-a; prefer composition
SC-199
- Q: What is a getter?
- A: Method accessed like a property: get prop() {}
SC-200
- Q: Singleton pattern?
- A: Ensure only one instance of a class exists