프로토타입 객체를 갖는 새 객체 생성. 파라미터로 새로 만들 객체의 프로토타입이어야 할 객체를 넘겨준다.
const carObj = {
showNumber : function() {
console.log("차 번호는 " + this.number);
}
}
const myCar = Object.create(carObj);
console.log(myCar);
// 실행 결과
[object Object] {
showNumber: function() {
window.runnerWindow.proxyConsole.log("차 번호는 " + this.number);
}
}
첫 번째 target Object에 다음 object들을 병합해 주는 것
Object.assign({target Obj}, {source Obj}, {source Obj}...);
const carObj = {
showNumber : function() {
console.log("차 번호는 " + this.number);
}
}
const myCar = Object.assign(Object.create(carObj), {
type: "Benz",
number: 1234
});
console.log(myCar);
// 실행 결과
{type: "Benz", number: 1234}
number: 1234
type: "Benz"
__proto__:
showNumber: ƒ ()
__proto__: Object
assign을 통해 만들어 지는 객체는 source Obj들에 같은 프로퍼티가 있을 경우나중에 오는 source Obj의 값으로 해당 프로퍼티가 덮혀진다.
또 assign을 통해 만들어 지는 객체는 프로퍼티와 그 값이 동일하다고 할 지라도 완전 다른 객체이기 때문에 === 비교를 해보면 false로 나온다.
target Obj에 뒤의 객체를 프로토타입으로 추가할 때 사용한다.
const carObj = {
showNumber : function() {
console.log("차 번호는 " + this.number);
}
}
const myCar = {
type: "Benz",
number: 1234
};
Object.setPrototypeOf(myCar, carObj); // myCar 객체에 carObj 객체를 프로토타입으로 추가해라.
console.log(myCar);
// 실행 결과
{type: "Benz", number: 1234}
number: 1234
type: "Benz"
__proto__:
showNumber: ƒ ()
__proto__: Object