我有以下代码:
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 :(得分: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;
}; /*
^ */