2016-05-13 15 views
0

Hallo bitte habe Geduld mit mir. Ich bin ein PHP-Entwickler, der in die Welt von javaScript eintritt. Jetzt baue ich ein Raster, das Blöcke unterschiedlicher Größe enthalten wird. Ich möchte einen Basisblock mit allen Methoden darauf und dann andere Blöcke, die dies erweitern (um seine Methoden zu verwenden), aber mit unterschiedlichen Eigenschaften. Ich habe viel gelesen, aber scheint nicht in der Lage zu sein, eine konsistente Methode zu finden. Ich habe einen Code zusammengestellt, obwohl ich funktionieren würde, aber nicht. Smallblock in dem Beispiel hat seine eigenen Eigenschaften, aber keinen von Blöcken EigenschaftenJavaScript Object Vererbungsprobleme

// Simple extend function to add for creating object inheritance 
    function extend(Child, Parent) { 
     var Temp = function(){ 
      Temp.prototype = Parent.prototype; 
      Child.prototype = new Temp(); 
      Child.prototype.constructor = new Child(); 
     } 
    } 

    //__________________________________________________ 
    // ## OBJECT DECLARATIONS ## 
    //__________________________________________________ 

    //__________________________________________________ 
    // ## BLOCK ## 
    // Base object representing a block/tile in the grid 
    var Block = function(){ 

     // The X position block 
     this.positionX = 0; 

     // The Y position block 
     this.positionY = 0; 

     // The height of the block 
     this.height = 0; 

     // The width of the block 
     this.width = 0; 

     // The horizontal orientation of a block 
     this.blockHorizontalOrientation = null; 

     // The vertical orientation of a block 
     this.blockVerticalOrientation = null; 

     // Method to set the width of a block 
     this.setWidth = function(width){ 
      this.width = width; 
     }; 

     // Method to set the height of a block 
     this.setHeight = function(height){ 
      this.height = height; 
     }; 

     // Method to get the width of a block 
     this.getWidth = function(){ 
      return this.width; 
     }; 

     // Method to get the height of a block 
     this.getHeight = function(){ 
      return this.height; 
     }; 

     // Method to set the X position of a block (in the grid) 
     this.setPosX = function(posX){ 
      this.positionX = posX; 
     }; 

     // Method to set the Y position of the block (in the grid) 
     this.setPosY = function(posY){ 
      this.positionY = posY; 
     }; 

     // Method to get the Y position of the clock 
     this.getPosY = function(){ 
      return this.positionY; 
     }; 

     // Method to get the X position of the clock 
     this.getPosX = function(){ 
      return this.positionX; 
     }; 

     // Method to get the horizontal orientation 
     this.getHOrientation = function(){ 
      return this.blockHorizontalOrientation; 
     }; 

     // Method to get the vertical orientation 
     this.getVOrientation = function(){ 
      return this.blockVerticalOrientation; 
     }; 

     // Method to get the horizontal orientation 
     this.setHOrientation = function(hOren){ 
      this.blockHorizontalOrientation = hOren; 
     }; 

     // Method to get the vertical orientation 
     this.setVOrientation = function(vOren){ 
      this.blockVerticalOrientation = vOren; 
     }; 
    } 

    //__________________________________________________ 
    // ## SMALL BLOCK ## 
    // object representing a small block/tile in the grid -- extends Block 
    var BlockSmall = function(){ 

     // The X position block 
     this.positionX = 0; 

     // The Y position block 
     this.positionY = 0; 

     // The height of the block 
     this.height = 1; 

     // The width of the block 
     this.width = 1; 

     // The horizontal orientation of a block 
     this.blockHorizontalOrientation = null; 

     // The vertical orientation of a block 
     this.blockVerticalOrientation = null; 
    }; 

    // EXTENDING BLOCK 
    extend(BlockSmall, Block); 

Bitte kann jemand diesen Code bewertet und hilft durch Beratung, was ich falsch mache. Bitte haben Sie wieder Geduld mit mir, wenn ich weit weg bin. Das sind alles brandneue Konzepte für mich.

Antwort

1

Nun, das sollte für inheriting arbeiten:

function extend(Child, Parent) { 
    Child.prototype = new Parent(); 
    Child.constructor = Child; 
} 

in Ihrem Code Sie nur eine Funktion innerhalb extend definieren, die nie aufgerufen wird.

+0

Vielen Dank !! Ich bin froh, dass ich nicht meilenweit weg war, das hat perfekt funktioniert. Ich kann jetzt sehen, dass ich Temp auf eine Funktion gesetzt habe, aber es wurde nicht wirklich aufgerufen. Ich glaube, ich habe diesen Code gestohlen. – Neil

+0

Ja, genau deshalb hat es nicht funktioniert. –