2015-09-09 56 views
0

这是我的第一个问题在stackoverflow:JavaScript基本错误;揭示模块图案样式

我有我的代码在Javascript中的问题。我在这个新的语言和书面方式,我不知道哪里是在这个模块中的问题,但这个错误是

Uncaught TypeError: Cannot read property 'init' of undefined.

我想首先HTML5/JS游戏,我有点紧张,因为这个问题我已经有几天了......谢谢,伙计们!

var game = (function() { 
 
    ////////////////////////////////////////////////// 
 
    /////////////////Atributos del juego///////////////// 
 
    //////////////////////////////////////////////////////// 
 
    var canvas = null, 
 
     ctx = null, 
 
     gameover = false, 
 
     pause = false, 
 
     score = 0; 
 

 
    ////////////////////////////////////////////////// 
 
    ////////////////////Métodos privados///////////////// 
 
    //////////////////////////////////////////////////////// 
 
    window.requestAnimationFrame = (function(callback) //Función autoejecutable que detecta la compatibilidad del navegador con la animación 
 
     { 
 
      return window.requestAnimationFrame || 
 
       window.webkitRequestAnimationFrame || 
 
       window.mozRequestAnimationFrame || 
 
       window.oRequestAnimationFrame || 
 
       window.msRequestAnimationFrame || 
 
       function(callback) { 
 
        window.setTimeout(callback, 1000/60); 
 
       }; 
 
     })(); 
 

 
    function loop() //Actualiza los estados y dibuja los elementos durante la partida 
 
    { 
 
     update(); 
 
     draw(); 
 
    } 
 

 
    function update() //Actualiza el estado del juego 
 
    { 
 
     window.requestAnimationFrame(update); 
 

 
    } 
 

 
    function draw() //Dibuja los elementos del juego en el canvas 
 
    { 
 
     ctx.drawImage(buffer, 0, 0); //Dibujamos el buffer en el contexto 
 
    } 
 

 
    ////////////////////////////////////////////////// 
 
    ////////////////////Métodos públicos///////////////// 
 
    //////////////////////////////////////////////////////// 
 
    function init() { 
 
     var i = 0; 
 
     alert(i); 
 
    } 
 

 
    return //Devuelve un objeto con todos los métodos públicos 
 
    { 
 
     init: init; 
 
    }; 
 
}()); 
 

 
game.init();

+0

的问题是在这条线'INIT:初始化;' - 你要它做什么?你在调用'init()'函数吗?函数解释:http://www.w3schools.com/js/js_functions.asp JavaScript和调试帮助有用的链接在这里http://stackoverflow.com/tags/javascript/info – Mousey

+0

这里的分号是一个语法错误: 'init:init;'。使用'init:init' – RobG

+1

@RobG在SO上没有规定我应该避免w3schools或任何其他来源的规则 - w3schools具有Try It功能,这非常方便。 – Mousey

回答

1

问题是与你的return语句,具体init分号后还有return关键字之后的换行符。

更改为:

return { 
    init: init 
}; 

var game = (function() { 
 
    ////////////////////////////////////////////////// 
 
    /////////////////Atributos del juego///////////////// 
 
    //////////////////////////////////////////////////////// 
 
    var canvas = null, 
 
     ctx = null, 
 
     gameover = false, 
 
     pause = false, 
 
     score = 0; 
 
    
 
    
 

 
    ////////////////////////////////////////////////// 
 
    ////////////////////Métodos privados///////////////// 
 
    //////////////////////////////////////////////////////// 
 
    window.requestAnimationFrame = (function(callback) //Función autoejecutable que detecta la compatibilidad del navegador con la animación 
 
     { 
 
      return window.requestAnimationFrame || 
 
       window.webkitRequestAnimationFrame || 
 
       window.mozRequestAnimationFrame || 
 
       window.oRequestAnimationFrame || 
 
       window.msRequestAnimationFrame || 
 
       function(callback) { 
 
        window.setTimeout(callback, 1000/60); 
 
       }; 
 
     })(); 
 

 
    function loop() //Actualiza los estados y dibuja los elementos durante la partida 
 
    { 
 
     update(); 
 
     draw(); 
 
    } 
 

 
    function update() //Actualiza el estado del juego 
 
    { 
 
     window.requestAnimationFrame(update); 
 

 
    } 
 

 
    function draw() //Dibuja los elementos del juego en el canvas 
 
    { 
 
     ctx.drawImage(buffer, 0, 0); //Dibujamos el buffer en el contexto 
 
    } 
 

 
    ////////////////////////////////////////////////// 
 
    ////////////////////Métodos públicos///////////////// 
 
    //////////////////////////////////////////////////////// 
 
    function init() { 
 
     var i = 0; 
 
     alert(i); 
 
    } 
 

 
    //Devuelve un objeto con todos los métodos públicos 
 
    return { 
 
     init: init 
 
    }; 
 
}()); 
 

 
game.init();

关于换行,见:Why doesn't a Javascript return statement work when the return value is on a new line?

+1

我在这里看不到解决方案? – Mousey

+0

@Mousey他发布了一个问题,在他的代码中抛出了一个特定的错误。这是该错误的来源。 –

+0

重复整个代码没有什么帮助,而且您还没有建议如何解决问题 - 只有问题出在哪里。 – Mousey

0

真正的问题是这样的

return // comment 
    { 
     init: init 
    }; 

由于自动分号插入的JavaScript分析器会将此解释为

return; 

您可以在Justifying Crockford claims阅读更多关于这一点,在“为什么每一个语句结尾;?

var app = document.getElementById('app'); 
 
var game = (function() { 
 
    function init() { 
 
     var i = 0; 
 
     app.innerHTML += '<p>game initialized</p>'; 
 
    } 
 
    // beware of http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/ 
 
    return { init: init } 
 
}()); 
 
game.init();
<div id="app"></div>