2016-04-11 51 views
0

我很難在一個類中調用一個函數。當我開始處理這個問題時,我已經運行了它,但是在修改之後,我無法工作:((
)您可以忽略我的警報,因爲我只是將它們放在那裏以檢查函數是否被調用。
我有功能「單位」,這是我的一個類。然後我用下面調用它。
從下面的代碼,這new Unit(args)沒有得到完成。它停在this.direction=this.setDirection();this.setDirection()不叫出於某種原因。任何人都可以看看,並告訴我哪裏出了問題?
謝謝!如何在課堂上運行功能。 Javascript


var teamBlack = []; 

var bking = new Unit("black", "king", new Coords(3,1, false)); 
teamBlack.push(bking); 

function Unit(team, type, coords) { 

    alert("Unit started " + count); 
    this.team = team; 
    this.type = type; 

    this.position = coords; 
    this.highlight = false; 
    alert("before setdirection"); 
    this.direction = this.setDirection(); 
    alert("this " + this.type); 

    this.setDirection = function() { 
     alert("setDirection Started "); 
     alert(this.type); 
     var tempDir = []; 
     switch (this.type) { 
      case "king" : 
       alert("this is king"); 
       tempDir = [this.N(), this.NW(), this.W(), this.SW(), this.S(), this.SE(), this.E(), this.NE()]; 
       break; 
      case "bishop" : 
       tempDir = [this.NW(), this.SW(), this.SE(), this.NE()]; 
       break; 
      case "rook" : 
       tempDir = [this.N(), this.S(), this.W(), this.E()]; 
       break; 
      case "pawn" : 
      { 
       if (this.team == "white") { 
        tempDir = [this.S()]; 
       } else { 
        tempDir = [this.N()]; 
       } 
       break; 
      } 
      case "queen" : 
      { 
       if (this.team == "white") { 
        tempDir = [this.N(), this.W(), this.SW(), this.S(), this.SE(), this.E()]; 
       } else { 
        tempDir = [this.N(), this.NW(), this.W(), this.S(), this.E(), this.NE()]; 
       } 
       break; 
      } 

     } 
     tempDir = tempDir.filter(function (dir) { 
      return dir.x > -1 && dir.x < 3 && dir.y > -1 && dir.y < 4 && dir.c == false; 
     }); 

     return tempDir; 
    } 
} 
+1

要調用* this.setDirection *您分配一個值之前。你不能那樣做。將它移到後面。你可能聽說過函數聲明被「掛起」,但是你對* setDirection *的賦值不是一個聲明,而是一個簡單的函數賦值給一個對象屬性。 – RobG

回答

2

此代碼錯誤,因爲您嘗試在您的課程中尚不存在的調用函數。我建議你在原型中移動這個函數。例如:

function Unit() { 
    //this is your constructor 
} 

Unit.prototype.setDirection = function() { 
    //your code of setDirection function 
} 
0

確保變量的定義,然後把this.setDirecton = function()...調用this.setDirection()之前。

請參閱JSFiddle

0

我會通過分離的職責提高代碼:

//create the class Unit 

    function Unit(team, type, coords) { 

     alert("Unit started " + count); 
     this.team = team; 
     this.type = type; 

     this.position = coords; 
     this.highlight = false; 
    } 

然後你的類指定的新方法:

Unit.prototype.setDirection = function() { 
     alert("setDirection Started "); 
     alert(this.type); 
     var tempDir = []; 
     switch (this.type) { 
      case "king" : 
       alert("this is king"); 
       tempDir = [this.N(), this.NW(), this.W(), this.SW(), this.S(), this.SE(), this.E(), this.NE()]; 
       break; 
      case "bishop" : 
       tempDir = [this.NW(), this.SW(), this.SE(), this.NE()]; 
       break; 
      case "rook" : 
       tempDir = [this.N(), this.S(), this.W(), this.E()]; 
       break; 
      case "pawn" : 
      { 
       if (this.team == "white") { 
        tempDir = [this.S()]; 
       } else { 
        tempDir = [this.N()]; 
       } 
       break; 
      } 
      case "queen" : 
      { 
       if (this.team == "white") { 
        tempDir = [this.N(), this.W(), this.SW(), this.S(), this.SE(), this.E()]; 
       } else { 
        tempDir = [this.N(), this.NW(), this.W(), this.S(), this.E(), this.NE()]; 
       } 
       break; 
      } 

     } 
     tempDir = tempDir.filter(function (dir) { 
      return dir.x > -1 && dir.x < 3 && dir.y > -1 && dir.y < 4 && dir.c == false; 
     }); 
     alert("before setdirection"); 
     this.direction = tempDir;//setMethod doesn't return thing but set something somewhere 
     alert("this " + this.type); 
    }; 

,現在你調用您的類的實例:

var teamBlack = []; 

var bking = new Unit("black", "king", new Coords(3,1, false)); 
bking.setDirection(); 
teamBlack.push(bking); 

我建議你看看this link

+0

在你的線後.. sorr我忘了打字..修理 –

0

您需要使用setDirection函數作爲Unit類的原型。

Unit.prototype.setDirection = function() { 
    //setDirection functionality 
} 

在原型中,您可以通過this訪問您的班級屬性。

入住此的jsfiddle:https://jsfiddle.net/urtr3quc/