Search code examples
javascriptclassnamespacesprototypeextend

How to extend a class inside a namespace in javascript?


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;
};

The next line produces the error (Object prototype undefined, has to be Object or null). As far as I can see this is because Shape is "namespaced".

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

How do I do this correctly?


Solution

  • You should use word prototype instead protoype.