TIL/JavaScript

7. Object.create()

han1693516 2023. 1. 2. 23:23

Object.create
   사용법
      - Object.create(연결시킬 객체의 프로토타입)
      - ex)
           const PersonProto = {
              calcAge(){
                  console.log(2037 - this.birthYear)
               }
           }

           const p1 = Object.create(PersonProto);
              - PersonProto와 p1이 prototypal inheritance로서 연결되어 있음
              - 이 때, p1은 현재 빈 객체

          cf> new Person() vs Object.create()
               1) 연결시킬 prototype를 정할 수 있음
                    const p1 = new Person()의 경우에 p1은 Person의 프로토타입과 자동으로 연결되지만,
                    Object.create는 연결시킬 프로토타입 객체를 정할 수 있음
               

'TIL > JavaScript' 카테고리의 다른 글

6. Static Method  (0) 2023.01.02
5. Getter & Setter  (0) 2023.01.02
4. 클래스 관련 TMI  (0) 2022.12.24
3. Prototype Chain  (0) 2022.12.24
2. OOP in JS & new operator  (0) 2022.12.24