严格模式下的函数的SyntaxError

时间:2014-06-03 22:23:12

标签: javascript

我正在处理以下代码并在renderBoard函数之后出现此错误。看起来一旦浏览器进入resetGame函数就会抛出此错误。我不完全理解为什么。

SyntaxError:在严格模式代码中,函数可以仅在顶层声明,也可以在另一个函数中立即声明。

function renderBoard() { 


var topRow = [$("0_0"), $("0_1"), $("0_2")];
    var middleRow = [$("1_0"), $("1_1"), $("1_2")];
    var bottomRow = [$("2_0"), $("2_1"), $("2_2")];for (var row = 0; row < 3; row++) {
   `enter code here` for(col = 0; col < 3; col++){

  var eltId = row + "_" + col;


        eltId = "0_" + col;//Why do you have to add the "0_" It goes row and column. Row O column 0. Row O column 1 Row 0 Column 3 
        if (topRow[col] == BLANK) {
                $(eltId).src = BLANK;
            }
            else if (topRow[col] == PLAYER_X) {
                $(eltId).src = PLAYER_X;
            } 
            else if (topRow[col] == PLAYER_O) {
                $(eltId).src = PLAYER_O;
            }
     }

    // middle row:
  for (var row = 0; row < 3; row++) {
    for(col = 0; col < 3; col++){
        eltId = "1_" + col;
        if (middleRow[col] == BLANK) {

                $(eltId).src = BLANK;
            }
            else if (middleRow[col] == PLAYER_X) {
               $(eltId).src = PLAYER_X;
            } 
            else if (middleRow[col] == PLAYER_O) {
                $(eltId).src = PLAYER_O;
            }
     }

    // bottom row:  
   for (var row = 0; row < 3; row++) {
    for(col = 0; col < 3; col++){
    {
        eltId = "2_" + col; //adds row number to column number eg. 2_0, 2_1, 2_2
        if (bottomRow[col] == BLANK) {

                $(eltId).src = BLANK;
            }
            else if (bottomRow[col] == PLAYER_X) {
                $(eltId).src = PLAYER_X;
            } 
            else if (bottomRow[col] == PLAYER_O) {
                $(eltId).src = PLAYER_O;
            }
     }
}



function resetGame(){
     `enter code here`var topRow = //added var to decalaration
     [BLANK,BLANK,BLANK];
     var middleRow = 
     [BLANK,BLANK,BLANK];
     var bottomRow =
     [BLANK,BLANK,BLANK];

    gameInProgress = true;
    updateDisplay();
    renderBoard();

}

1 个答案:

答案 0 :(得分:0)

你错过了一堆关闭的大括号'}'
试试这个:

function renderBoard() { 


var topRow = [$("0_0"), $("0_1"), $("0_2")];
var middleRow = [$("1_0"), $("1_1"), $("1_2")];
var bottomRow = [$("2_0"), $("2_1"), $("2_2")];for (var row = 0; row < 3; row++) {
     for(col = 0; col < 3; col++){

        var eltId = row + "_" + col;


        eltId = "0_" + col;// Why do you have to add the "0_" It goes row and
        // column. Row O column 0. Row O column 1 Row 0
        // Column 3
        if (topRow[col] == BLANK) {
            $(eltId).src = BLANK;
        }
        else if (topRow[col] == PLAYER_X) {
            $(eltId).src = PLAYER_X;
        } 
        else if (topRow[col] == PLAYER_O) {
            $(eltId).src = PLAYER_O;
        }
    }

    // middle row:
    for (var row = 0; row < 3; row++) {
        for(col = 0; col < 3; col++){
            eltId = "1_" + col;
            if (middleRow[col] == BLANK) {

                $(eltId).src = BLANK;
            }
            else if (middleRow[col] == PLAYER_X) {
                $(eltId).src = PLAYER_X;
            } 
            else if (middleRow[col] == PLAYER_O) {
                $(eltId).src = PLAYER_O;
            }
        }

        // bottom row:
        for (var row = 0; row < 3; row++) {
            for(col = 0; col < 3; col++){
                {
                    eltId = "2_" + col; // adds row number to column number eg. 2_0, 2_1,
                    // 2_2
                    if (bottomRow[col] == BLANK) {

                        $(eltId).src = BLANK;
                    }
                    else if (bottomRow[col] == PLAYER_X) {
                        $(eltId).src = PLAYER_X;
                    } 
                    else if (bottomRow[col] == PLAYER_O) {
                        $(eltId).src = PLAYER_O;
                    }
                }
            }
        }                
    }
}
}

function resetGame(){
     var topRow = //added var to decalaration
     [BLANK,BLANK,BLANK];
     var middleRow = 
     [BLANK,BLANK,BLANK];
     var bottomRow =
     [BLANK,BLANK,BLANK];

    gameInProgress = true;
    updateDisplay();
    renderBoard();

}

除此之外,enter code here还会产生语法错误。