Object-Oriented Programming

1. What is Object-Oriented Programming

  • Object-oriented programming bundles data together for processing, unlike procedural programming

  • Properties and methods are included in one concept called 'object', which is different from JavaScript's built-in object type and is called a class

  • Object-oriented programming creates a blueprint that serves as a model β†’ class

  • The prototype (original form) used when creating the model's blueprint β†’ prototype

  • A programming pattern that creates objects (instances) based on that blueprint

2. Class Module Pattern

Method Call

let counter1 = {
  value: 0;
  increase: function() {
    this.value++ // When calling a method, this refers to counter1
  },
  decrease: function() {
    this.value--
  },
  getValue: function() {
    return this.value
  }
}

counter1.increase()
counter1.decrease()
counter1.increase()
counter1.decrease()
counter1.getValue() // 2

Creating New Objects Each Time Using Closure

3. Class and Instance

Syntax for Creating Classes: class keyword

Creating Class Instances

this: Instance Object

  • Brand, name, color, etc. passed as parameters are values specified when creating instances

  • Assigning to this means 'giving' the created instance that brand, name, and color

  • A unique execution context created for each scope when a function runs

  • When creating an instance with the new keyword, that instance becomes the this value

Method Definition

Define together with constructor function inside the class keyword

Usage in instances: How to use the properties and methods set above in instances

Summary

4. Object-Oriented Programming

Procedural Language > Object-Oriented Language

  • Procedural language: Combination of sequential commands

  • Object-oriented language: Code is written using class as a blueprint for data models, with data and functionality bundled together for processing

  • JavaScript is not an object-oriented language, but can be written in object-oriented patterns

Four Basic Concepts of OOP

(1) Encapsulation

  • Bundling data and functionality into one unit

  • Hiding: Hide implementation, expose behavior

  • Favorable for Loose Coupling: Instead of writing code procedurally according to execution order, bundle code to resemble the actual appearance it represents, enabling implementation modifications anytime

  • Makes code less complex and increases reusability

(2) Inheritance

  • Child, or derived class, inherits characteristics from parent, or base class

  • Makes code simple and increases reusability

(3) Abstraction

  • Internal implementation is complex but the actually exposed part is made simple to prevent unexpected usage changes

  • Encapsulation focuses on hiding data, while abstraction focuses on not exposing unnecessary methods to people using the class and defining them with simple names

  • Makes code less complex and minimizes impact on changes

(4) Polymorphism

  • ex) Methods with the same name are applied slightly differently to each element

  • Instead of conditional statements like if/else for the same method, can write differently according to each object's characteristics

5. Prototype

Prototype and Class

JavaScript is a prototype-based language, and prototype means original object

Prototype Chain

When implementing inheritance, one of the characteristics of object-oriented programming, use prototype chain

Last updated