본문 바로가기

Coding/javascript

Object.create() 란

ES5

ES5 문법이다

ES6 에서는 new 가 있으니..

 

The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.

 

새로 생성될 객체의 프로토타입 객체를 넣어주면, 새로운 객체가 생성됨

 

const person = {
  isHuman: false,
  printIntroduction: function () {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  }
};

const me = Object.create(person);

me.name = "Matthew"; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // inherited properties can be overwritten

me.printIntroduction();
// expected output: "My name is Matthew. Am I human? true"