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;
}
你可以發佈給你錯誤的版本嗎? –
嘿羅布,編輯! :) – Kai
上一行似乎有一個缺失分號。 – Bergi