A skill for understanding and applying Object-Oriented Programming (OOP) concepts such as classes, inheritance, encapsulation, and polymorphism, with a focus on how they translate to JavaScript.
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data (properties) and code (methods). It is a fundamental paradigm for building complex, modular, and maintainable systems.
Classes serve as templates or "blueprints" for creating objects. An instance is a specific object created from a class template.
class Professor {
properties: name, teaches
constructor(name, teaches)
methods: grade(paper), introduceSelf()
}
// Creating an instance
walsh = new Professor("Walsh", "Psychology");
Inheritance allows a class (subclass) to derive properties and methods from another class (superclass). This promotes code reuse and helps model "is-a" relationships.
class Person {
properties: name
methods: introduceSelf()
}
class Professor extends Person {
properties: teaches
methods: grade(paper)
}
class Student extends Person {
properties: year
}
Polymorphism is the ability of different classes to provide their own implementation of a method that has the same name.
A Professor and a Student are both Person instances, but they introduce themselves differently. When you call .introduceSelf(), the system uses the implementation specific to the object's class.
Encapsulation is the practice of keeping an object's internal state private and only exposing a controlled public interface.
While JavaScript has a class syntax, its underlying mechanism is different from "classical" OOP languages like Java or C++.
[[Prototype]]) that points to another object.class keyword in JavaScript is primarily "syntactic sugar" over the existing prototype-based inheritance mechanism.