Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中扩展命名空间内的类?_Javascript_Class_Namespaces_Prototype_Extend - Fatal编程技术网

如何在javascript中扩展命名空间内的类?

如何在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点我才看

下一行生成错误(对象原型未定义,必须为Object或null)。据我所知,这是因为形状是“名称空间”


我如何正确地做到这一点?

你应该使用单词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();