2016-09-17 1 views
-1
var sl = sl || {} 

sl.Shape = function(){ 
    this.x = 0; 
    this.y = 0; 
}; 
sl.Shape.prototype.move = function(x,y){ 
    this.x += x; 
    this.y += y; 
}; 
sl.Rectangle = function(){ 
    sl.Shape.call(this); 
    this.z = 0; 
}; 

다음 줄은 Object prototype undefined이거나 Object 또는 null이어야하는 오류를 생성합니다. 내가 볼 수있는 한 Shape은 "네임 스페이스"이기 때문입니다.자바 스크립트에서 네임 스페이스 내부의 클래스를 확장하는 방법은 무엇입니까?

sl.Rectangle.protoype = Object.create(sl.Shape.protoype); 
sl.Rectangle.protoype.constructor = sl.Rectangle; 

어떻게 올바르게 수행 할 수 있습니까?

답변

1

프로토 타입 대신 단어 프로토 타입을 사용해야합니다.

+0

감사 쓴! – Daniela

0

당신은 안드리는 지적이 예를 시도 단어 "프로토 타입"맞춤법이 틀린있다 : 심지어 당신이 무엇에, 볼 4 시까 지 걸렸다,

(function() { 
    var sl = sl || {}; 

    function Shape() { 
    this.x = 0; 
    this.y = 0; 
    } 

    Shape.prototype.move = function(x, y) { 
    this.x += x; 
    this.y += y; 
    }; 

    function Rectangle() { 
    Shape.apply(this, arguments); 
    this.z = 0; 
    }; 

    Rectangle.prototype = Object.create(Shape.prototype); 
    Rectangle.prototype.constructor = Rectangle; 

    sl.Shape = Shape; 
    sl.Rectangle = Rectangle; 

    // expose 
    window.sl = sl; 
}()); 

사용

var shape = new sl.Shape(); 
var rect = new sl.Rectangle(); 
+0

감사합니다. 더 우아 해 보입니다. – Daniela

관련 문제