如何在Javascript中扩展类/原型

时间:2016-04-27 15:47:41

标签: javascript design-patterns javascript-objects

好的,所以我的主类的模块模式如下:

    Var draggable = {};

    (function(){

     var initialMouseX = undefined;
     var initialMouseY = undefined;
     var startX        = undefined;
     var startY        = undefined;
     var draggedObject = undefined;

     var startDragX    = undefined;
     var startDragY    = undefined;

     var eventHandler  = undefined;
     var canvas        = undefined;

     var that = this;

     this.setCanvasObject = function(cnv){
         canvas = cnv;
     }

     this.setEventHandler = function(eH){
         eventHandler = eH;
     };

     this.setClickable = function(element){
         ...        
     };

     this.setDraggable = function(element){
         ...
     };

     this.startDragMode = function(ev){
        ...
     };

     this.dragMouse = function(ev){
       ....
     };

     this.setPosition = function(dX, dY){
       ...
     };

     this.releaseElement = function() {
      ...
     };
 }).apply(draggable);

这是我的父类或原型类,使用我的模块设计模式。

这是我的子类,使用相同的模块模式:

var toolbarHeader = {};

 (function(){

         this.__proto__ = draggable;

         var that = this;

         this.__proto__.startDragMode = function(ev){
             var evt = ev || window.event;
             var target = evt.target || evt.srcElement;

             target = target.parentNode;
             that.startDrag(target);
             initialMouseX = evt.clientX;
             initialMouseY = evt.clientY;

             eventHandler.addEventSimple(document,'mousemove', that.dragMouse);
             eventHandler.addEventSimple(document,'mouseup', that.releaseElement);                    
         };


 }).apply(toolbarHeader);

我想要做的是使用toolbarHeader类来覆盖父方法,并在其余时间使用parent方法。

目前它发出错误,因为它无法找到that.releaseElement,that.startDrag和that.dragMouse。

我做错了什么?这是我应该如何使用继承?这是一个很好的设计吗?

0 个答案:

没有答案