Hello World...
Object.create(obj) 와 new 차이? 본문
Object.create 와 new 가 차이가 없는 줄 알았는데, 사실 큰 차이가 있었다.
new Car() 하면 당연히 생성자가 실행이 되고 Object.create(Car.prototype) 을 하면 __proto__ 로 연결만 되는 것 같다.
function Car() {
this.max = 350;
this.auto = true;
}
Car.prototype.run = function() {
console.log('run~~~');
}
function ToyCar() {
Car.call(this)
this.model = "toy";
}
ToyCar.prototype = new Car();
ToyCar.prototype.constructor = ToyCar;
// ===console====
// Car {max: 350, auto: true}
-----------------
ToyCar.prototype = Object.create(Car.prototype);
ToyCar.prototype.constructor = ToyCar;
// ===console====
//Car {}
'javascript' 카테고리의 다른 글
replaceAll method 만들기 (0) | 2020.01.16 |
---|---|
encodeURIComponent (0) | 2020.01.14 |
자바스크립트 CLASS (0) | 2020.01.03 |
자바스크립트 객체 참조 (0) | 2020.01.02 |
클로저 객체 메소드 (0) | 2020.01.01 |
Comments