분류 javascript

JavaScript create() 메소드 이해하기

컨텐츠 정보

  • 조회 305 (작성일 )

본문

사용법 : const newObject = Object.create(prototype)


const animal = {} 

const cat = Object.create(animal)

==================================================

지정된 프로토 타입으로 새 객체를 만듭니다.


새로 생성 된 객체는 모든 prototyope 객체 속성을 상속받습니다.


두 번째 매개 변수를 지정하여 프로토타입이 부족한 새 속성을 객체에 추가 할 수 있습니다.


const newObject = Object.create(prototype, newProperties) 

여기서 newProperties는 각 속성을 정의하는 객체의 객체입니다.


const animal = {} 

const cat = Object.create(animal, {

   breed: {

     value: 'Tiger cat'

   }

 }); 


console.log(cat.breed)   //'Tiger cat'