【问题标题】:SyntaxError for functions in strict mode严格模式下函数的 SyntaxError
【发布时间】:2014-07-24 10:15:16
【问题描述】:

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

语法错误:在严格模式代码中,函数只能在顶层或直接在另一个函数中声明。

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

}

【问题讨论】:

    标签: javascript


    【解决方案1】:

    你缺少一堆右括号'}'
    试试这个:

    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 还会产生语法错误。

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 2014-05-01
      • 2012-08-26
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 2016-12-31
      • 2016-11-20
      相关资源
      最近更新 更多