如何在javascript中扩展命名空间内的类?
下一行生成错误(对象原型未定义,必须为Object或null)。据我所知,这是因为形状是“名称空间”如何在javascript中扩展命名空间内的类?,javascript,class,namespaces,prototype,extend,Javascript,Class,Namespaces,Prototype,Extend,下一行生成错误(对象原型未定义,必须为Object或null)。据我所知,这是因为形状是“名称空间” 我如何正确地做到这一点?你应该使用单词prototype而不是prototype。正如安德烈所指出的,你拼错了单词prototype,试试这个例子: sl.Rectangle.protoype = Object.create(sl.Shape.protoype); sl.Rectangle.protoype.constructor = sl.Rectangle; 用法 谢谢,直到凌晨4点我才看
我如何正确地做到这一点?你应该使用单词prototype而不是prototype。正如安德烈所指出的,你拼错了单词prototype,试试这个例子:
sl.Rectangle.protoype = Object.create(sl.Shape.protoype);
sl.Rectangle.protoype.constructor = sl.Rectangle;
用法
谢谢,直到凌晨4点我才看到它,即使是在你写的东西里!!
sl.Rectangle.protoype = Object.create(sl.Shape.protoype);
sl.Rectangle.protoype.constructor = sl.Rectangle;
(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();