본문 바로가기

Code/web-frontend

Object.create() 란

ES5

Object.create() 는 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"