JavaScript
Result
Run
License
// 부모 객체의 생성자 function Shape() { this.x = 0; this.y = 0; } // 부모 객체의 새로운 메소드 추가 Shape.prototype.move = function(x, y) { this.x += x; this.y += y; console.log("Moved!"); }; // 부모를 상속받을 새로운 객체를 생성하는 생성자 function Rectangle(width, height) { // 부모 생성자 호출 Shape.call(this); this.width = width; this.height = height; } // 부모 객체를 상속하여 확장 Rectangle.prototype = Object.create(Shape.prototype); // 생성자를 설정 Rectangle.constructor = Rectangle; // 인스턴스 생성 var rect = new Rectangle(100, 200); // 메소드 호출 rect.move(100, 60); // 인스턴스를 체크 console.log("rect는 Rectangle의 instance인가? " + (rect instanceof Rectangle)); console.log("rect는 Shape의 instance인가? " + (rect instanceof Shape));
Console
expand_less
License
License
by DevDic
Close