create

es버전
Tags
상속
잘안씀
비고
새로운 오브젝트 생성 - 고전적인 상속 구현에 쓰임
Object.create(proto[, propertiesObject])
지정된 프로토타입 객체 및 속성(property)을 갖는 새 객체를 만듭니다.

인자

proto
: 새로 만든 객체의 프로토타입이어야 할 객체.
propertiesObject Optional
: 지정되고 undefined가 아니면, 자신의 속성(즉, 자체에 정의되어 그 프로토타입 체인에서 열거가능하지 않은 속성)이 열거가능한 객체는 해당 속성명으로 새로 만든 객체에 추가될 속성 설명자(descriptor)를 지정합니다. 이러한 속성은 Object.defineProperties()의 두 번째 인수에 해당합니다.
 

고전적인 상속방법

// Shape - 상위클래스 function Shape() { this.x = 0; this.y = 0; } // 상위클래스 메서드 Shape.prototype.move = function(x, y) { this.x += x; this.y += y; console.info('Shape moved.'); }; // Rectangle - 하위클래스 function Rectangle() { Shape.call(this); // super 생성자 호출. } // 하위클래스는 상위클래스를 확장 Rectangle.prototype = Object.create(Shape.prototype); Rectangle.prototype.constructor = Rectangle; var rect = new Rectangle(); console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle); // true console.log('Is rect an instance of Shape?', rect instanceof Shape); // true rect.move(1, 1); // Outputs, 'Shape moved.'