gpt4 book ai didi

javascript - 自调用JS函数

转载 作者:行者123 更新时间:2023-12-03 07:42:41 26 4
gpt4 key购买 nike

我有以下代码:

        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 (){ 位于代码块之前。
})(); 在代码块后面。

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

Uncaught TypeError: (intermediate value)(intermediate值)(中间值)(...)不是函数(...)。

我似乎找不到这里的问题。有人可以告诉我发生了什么事吗?

当前版本:

(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;
}

最佳答案

您的 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;
}; /*
^ */

关于javascript - 自调用JS函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com