簡體   English   中英

嚴格模式下函數的SyntaxError

[英]SyntaxError for functions in strict mode

我正在處理以下代碼,並在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();

}

您缺少一串右大括號'}'
嘗試這個:

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還會產生語法錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM