2016-02-11 40 views
0

我有以下代码:自调用JS功能

 var currentKey = 0; 
     var totalBinaryMultiplesCollection = {}; 
     for (var row in playField) { 
      if (playField.hasOwnProperty(row)) { 
       alert(row + " -> " + playField[row]); 
       var rowLength = playField[row].length; 
       //Call rowCalc function which returns an array with the binary nrs used in calc 
       var binaryMultiplesRow = rowCalc(rowLength); 
       for(j=0; j < binaryMultiplesRow.length; j++){ 
        //Two methods 
        totalBinaryMultiplesCollection[currentKey] = binaryMultiplesRow[j]; 
        currentKey+=1; 
       } 
      } 
     } 

我想改变这个代码是自调用函数。所以我加了下面的东西:

(function(){之前的代码块。
})();后面的代码块。

然而,这给了我以下错误:

未捕获TypeError :(中间值)(中间值)(中间值)(...)不是函数(...)。

我似乎无法在这里找到问题。有人能告诉我发生了什么事吗?

当前版本:

(function() { 
     var currentKey = 0; 
     var totalBinaryMultiplesCollection = {}; 
     for (var row in playField) { 
      if (playField.hasOwnProperty(row)) { 
       alert(row + " -> " + playField[row]); 
       var rowLength = playField[row].length; 
       //Call rowCalc function which returns an array with the binary nrs used in calc 
       var binaryMultiplesRow = rowCalc(rowLength); 
       for(j=0; j < binaryMultiplesRow.length; j++){ 
        //Two methods 
        totalBinaryMultiplesCollection[currentKey] = binaryMultiplesRow[j]; 
        currentKey+=1; 
       } 
      } 
     } 
    })(); 

和rowCalc被调用函数:

var rowCalc = function(rowlength){ 
     var currentRowCollection = []; 
     switch(rowlength) { 
     case 1: 
      currentRowCollection.push(1); 
      break; 
     case 2: 
      currentRowCollection.push(2); 
      break; 
     case 3: 
      currentRowCollection.push(1); 
      currentRowCollection.push(2); 
      break; 
     case 4: 
      currentRowCollection.push(4); 
      break; 
     case 5: 
      currentRowCollection.push(1); 
      currentRowCollection.push(4); 
      break; 
     case 6: 
      currentRowCollection.push(2); 
      currentRowCollection.push(4); 
     case 7: 
      currentRowCollection.push(2); 
      currentRowCollection.push(4); 
      currentRowCollection.push(1); 
      break; 
     default: 
      alert("You made a mistake!") 
     } 
     return currentRowCollection; 
    } 
+0

你可以发布给你错误的版本吗? –

+0

嘿罗布,编辑! :) – Kai

+0

上一行似乎有一个缺失分号。 – Bergi

回答

1

,我们在您rowCalc功能两人失踪分号,其中第二个是导致错误:

var rowCalc = function(rowlength){ 
    var currentRowCollection = []; 
    switch(rowlength) { 
    case 1: 
     currentRowCollection.push(1); 
     break; 
    case 2: 
     currentRowCollection.push(2); 
     break; 
    case 3: 
     currentRowCollection.push(1); 
     currentRowCollection.push(2); 
     break; 
    case 4: 
     currentRowCollection.push(4); 
     break; 
    case 5: 
     currentRowCollection.push(1); 
     currentRowCollection.push(4); 
     break; 
    case 6: 
     currentRowCollection.push(2); 
     currentRowCollection.push(4); 
    case 7: 
     currentRowCollection.push(2); 
     currentRowCollection.push(4); 
     currentRowCollection.push(1); 
     break; 
    default: 
     alert("You made a mistake!"); 
//        ^
    } 
    return currentRowCollection; 
}; /* 
^ */