ES6 클래스에 대한 심층 분석

클래스는 ECMAScript 6에서 도입되었으며 개체 생성을 위한 템플릿을 정의하여 기존 OOP 방식으로 코드를 구성하는 데 클래스를 사용할 수 있습니다.
이 게시물에서 우리는 ES6 클래스에 대한 모든 것을 배운 다음 생성자 함수 및 프로토타입 상속과 비교할 것입니다.

A quick word before we start. This article is intended to appeal to a wide range of readers. So, if you're an advanced JS user, you can use the table of contents below to select which sections to read. If, on the other hand, you're just getting started with JS and you're having trouble understanding something, feel free to ask me in the comments section.


목차



  • Anatomy of a class
  • The class keyword
  • Constructor
  • Properties
  • Methods
  • Getters and Setters
  • Static fields and methods


  • Creating an object with a class
  • The new keyword
  • Hoisting


  • Inheritance
  • The extends keyword
  • Super
  • Metadata


  • Comparison with Constructor functions
  • Properties and methods
  • Getters and Setters
  • Prototypal inheritance

  • Wrap up

  • 클래스를 정의하는 방법과 클래스를 사용하여 개체를 만드는 방법을 살펴본 다음 상속 등에 대해 이야기할 것입니다. 하지만 먼저 클래스의 구조를 살펴보는 것으로 바로 시작하겠습니다.

    클래스 분석

    클래스 키워드

    To declare a class we use the class keyword followed by the name of the class.

    클래스 선언

    class Point {
      constructor() {}
    }
    

    In the snippet above we declared a "Point" class. This is called a class declaration.

    Note that I'm using the PascalCase notation for the name of the class. This is not mandatory but a common convention.

    In fact classes are special functions, and like with functions, you can use either class declarations or class expressions.

    클래스 표현

    This is a class expression:

    let Point = class {
      constructor() {}
    }
    

    건설자

    The constructor method is a special method for creating and initialising an object created with a class.

    There can only be one constructor in each class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor.

    It is not mandatory to have a constructor in the class definition. The code bellow is valid.

    class Point { }
    

    속성

    인스턴스 속성

    Instance properties must be defined inside of class methods. In the snippet below x and y are instance properties:

    class Point {
      constructor(a, b) {
        this.x = a;
        this.y = b;
      }
    }
    

    필드

    The code can be more self documenting by declaring fields up-front. Let's refactor the code above using fields, and while we're at it, let's give them a default value:

    class Point {
      x = 0;
      y = 0;
    
      constructor(a, b) {
        this.x = a;
        this.y = b;
      }
    }
    

    Note that fields are always present whereas instance properties must be defined inside of class methods.
    Note also that fields can be declared with or without a default value.

    비공개 필드

    To declare a private field all you have to do is prefix its name with # . See the code below:

    class Point {
      #x = 0;
      #y = 0;
    
      constructor(a, b) {
        this.#x = a;
        this.#y = b;
      }
    }
    

    Trying to access a private field outside the scope of the class will result in a syntax error.

    Note that instance properties can not be private, only fields can. So you can't create an instance property with the # prefix. This would result in a syntax error.

    행동 양식

    공개 메서드

    To declare a method we can use the ES6 shorter syntax for method definitions on objects:

    class Point {
      #x = 0;
      #y = 0;
    
      translate(a, b) {
        this.#x += a;
        this.#y += b;
      }
    }
    

    비공개 방법

    Like we did with private fields, we can use a # as a prefix of our private methods:

    class Point {
      #x = 0;
      #y = 0;
    
      constructor(x, y) {
        this.#setXY(x, y)
      }
    
      translate(a, b) {
        this.#setXY(
          this.#x + a,
          this.#y + b);
      }
    
      // Private method
      #setXY(x, y) {
        this.#x = x;
        this.#y = y;
      }
    }
    

    생성기 방법

    The same way as public methods we can declare generator methods:

    class Point {
      #x = 0;
      #y = 0;
      #historyPositions = [];
    
      translate(a, b) {
        this.#x += a;
        this.#y += b;
    
        this.#historyPositions.unshift(
          [this.#x, this.#y]
        );
      }
    
      *getHistoryPositions() {
        for(const position of this.#historyPositions){
          yield position;
        }
      }
    }
    

    In the snippet above we declared a getHistoryPositions generator method.

    Note: to declare a private generator method use this syntax: *#getHistoryPositions() {}.

    게터와 세터

    To implement getters and setters we use the get and set keyword:

    Here is an example:

    class Point {
      #x = 0;
      #y = 0;
    
      get position() {
        return [this.#x, this.#y];
      }
    
      set position(newPosition) {
        // newPosition is an array like [0, 0]
        [this.#x, this.#y] = newPosition;
      }
    }
    

    정적 필드 및 메서드

    Static methods and fields (of a class) can be defined using the static keyword. Static members (fields and methods) cannot be called through a class instance and must be called without instantiating the class.

    Static methods are frequently used to construct utility functions, whereas static properties are excellent for caching, fixed-configuration, or any other data that does not need to be copied across instances.

    Here is an example of a static method:

    class Point {
      static isEqual(pointA, pointB) {
        const [x1, y1] = pointA.position;
        const [x2, y2] = pointB.position;
        return x1 === x2 && y1 === y2;
      }
    
      #x = 0;
      #y = 0;
    
      get position() {
        return [this.#x, this.#y];
      }
    
      constructor(a, b) {
        [this.#x, this.#y] = [a, b];
      }
    }
    
    // Consider that p1 and p2 are both instances of Point
    Point.isEqual(p1, p2) // Boolean
    

    클래스로 객체 생성

    새로운 키워드

    To create a new instance of a class we use the new keyword:

    class Point {}
    
    const point = new Point();
    

    게양

    Function declarations and class declarations can be distinguished by the fact that function declarations are hoisted whereas class declarations are not. You must first define and then access your class; otherwise, code like this will throw a ReferenceError:

    const point = new Point(); // ReferenceError
    
    class Point {}
    

    계승

    확장 키워드

    In class declarations or class expressions, the extends keyword is used to create a class that is a child of another class (a subclass).
    We'll look at an example in the next section.

    감독자

    The super keyword is used to access and call functions on an object's parent.
    If there is a constructor present in the subclass, it needs to first call super() before using this .

    See the code below:

    class Vehicle {
      #numberOfPassengers = 0;
    
      constructor(nb) {
        this.#numberOfPassengers = nb;
      }
    
      getNumberOfPassengers() {
        return this.#numberOfPassengers;
      }
    }
    
    class Car extends Vehicle {
      constructor() {
        super(5);
      }
    }
    
    class Bike extends Vehicle {
      constructor() {
        super(1);
      }
    }
    
    const car = new Car();
    const bike = new Bike();
    
    car.getNumberOfPassengers(); // 5
    bike.getNumberOfPassengers(); // 1
    

    메타데이터

    In class constructors, new.target refers to the constructor that was called directly by new. This is also true if the constructor belongs to a parent class and was delegated from a child constructor.

    class Vehicle {
      constructor() {
        console.log(new.target.name);
      }
    }
    
    class Car extends Vehicle {
      constructor() {
        super();
      }
    }
    
    new Vehicle(); // Vehicle
    new Car(); // Car
    

    Consider the following use case: If we want the Vehicle class to be abstract, we can throw an error if (new.target.name === 'Vehicle') is true. However, you've to keep in mind that if you use this in production and build your project with bundlers, the names of your classes may be prefixed, causing the condition to always be false.

    생성자 함수와의 비교

    Before there were classes, constructor functions and prototypes were the default. I won't go too deep in this section, but i wanted to show you how we could achieve pretty much the same with constructor functions and prototypes since ES6 classes use prototypes behind the hood.

    속성 및 메서드

    Let's start by setting some properties and methods:

    function Point(x, y) {
      this.x = x;
      this.y = y;
    
      this.translate = function(a, b) {
        this.x += a;
        this.y += b;
      }
    }
    
    const point = new Point(4, 5);
    point.translate(2, 2);
    point.x; // 6
    point.y; // 7
    

    게터와 세터

    To implement setters and getters we have to use Object.defineProperty or Object.defineProperties :

    function Point(x, y) {
      this.x = x;
      this.y = y;
    
      Object.defineProperty(this, 'position', {
        set: function([x, y]) {
          [this.x, this.y] = [x, y];
        },
        get: function() {
          return [this.x, this.y];
        },
      });
    }
    
    const point = new Point();
    point.position = [4, 5];
    point.position; // [4, 5]
    

    Basically, I used Object.defineProperty to set/change the property descriptor of the position property. To learn more about property descriptors, you can check this article:





    프로토타입 상속

    Here's an example of prototypal inheritance:

    function Vehicle(numberOfPassengers) {
      this.numberOfPassengers = numberOfPassengers;
    
      this.getNumberOfPassengers = function() {
        return this.numberOfPassengers;
      }
    }
    
    function Car() {
      Vehicle.call(this, 5); // The same way we used super for classes, here we call the Vehicle constructor in this context (Car context) 
    }
    
    Car.prototype = Object.create(Vehicle.prototype); // Setting up the inheritance
    Car.prototype.constructor = Car; // As a side effect of the line above, we loose the Car constructor. So we have to set it back
    
    const car = new Car();
    car.getNumberOfPassengers(); // 5
    

    I won't go into much details here as there's a lot to talk about. But this is the minimal setup to do prototypal inheritance.

    You may agree with me or not, but I find it a lot less straight forward and less descriptive than the class implementation.

    마무리

    We covered a lot already. We saw all of the tools we can use to create classes that are tailored to our needs, we discussed how to create objects using classes and we talked about some caveats to be aware of. Finally we saw how difficult it can be to use constructor functions compared to using classes.

    That's it for this post. I hope you liked it. If you did, please share it with your friends and colleagues. Also you can follow me on twitter at as it would greatly help me.

    Have a good day !


    다음에 읽을 내용은?




    좋은 웹페이지 즐겨찾기