我有以下代码:
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;
}