简体   繁体   English

有效JavaScript的TypeScript错误

[英]TypeScript error from valid JavaScript

The below code is valid JavaScript, yet in typescript it throws the following error. 以下代码是有效的JavaScript,但在打字稿中会引发以下错误。 Any idea why? 知道为什么吗?

Error 7 The name 'gameloop' does not exist in the current scope 错误7名称“ gameloop”在当前范围中不存在

(function gameloop() {
    window.requestAnimationFrame(gameloop);
})();

EDIT: After first answer noticed copy and paste error that wasn't related to actual problem. 编辑:第一个答案后注意到与实际问题无关的复制和粘贴错误。

That's just a missing semicolon, preventing it to be correct both in Javascript and Typescript. 那只是一个缺少的分号,从而使其在Javascript和Typescript中都无法正确使用。

This should work : 这应该工作:

   var game = new Game(); // <== don't forget ';' here
   (function gameloop() {

       // stats.update();
        game.step();
        game.update();
        game.draw();

        window.requestAnimationFrame(gameloop);
    })();

Don't rely on automatic semicolon insertion : rules are too complex. 不要依靠自动分号插入:规则太复杂了。

In your case, the compiler was seeing 在您的情况下,编译器发现

... new Game()(something) ...

and it hadn't enough information not to think that something was the arguments passed to the function new Game() . 并且它没有足够的信息来不认为传递给函数new Game()的参数是something Hence the error... 因此错误...

Got it. 得到它了。 The error was to do with scope of the function. 错误与功能范围有关。 So I need to have this 所以我需要这个

(function gameloop() {
window.requestAnimationFrame(this.gameloop);
})();

even though it complies down to this in Javascript. 即使它符合Javascript的要求。

(function gameloop() {
window.requestAnimationFrame(gameloop);
})();

I have run into this also, but it required a different fix. 我也遇到了这个问题,但是它需要另外解决。 In my case, I was using window.requestAnimationFrame from TypeScript, then running the code on Chrome. 就我而言,我使用TypeScript中的window.requestAnimationFrame,然后在Chrome上运行代码。 However, I got a runtime error, "window does not have requestAnimationFrame". 但是,出现运行时错误,“窗口没有requestAnimationFrame”。 This is because requestAnimationFrame is still prefixed in Chrome. 这是因为requestAnimationFrame在Chrome中仍然是前缀。 You will either need to add a prefixed declaration to your TypeScript declarations OR just add a JavaScript file to your project that plolyfills window.requestAnimationFrame in the standard way; 您将需要在TypeScript声明中添加前缀声明,或者只是将JavaScript文件添加到您的项目中,以标准方式展开window.requestAnimationFrame;

// requestAnimFrame shim  with setTimeout fallback
window.requestAnimationFrame = (function () {
    return window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function (callback) {
                window.setTimeout(callback, 1000 / 60);
            };
})();

I used the polyfill method to make requestAnimationFrame work on chrome, since it is such a common fix. 我使用polyfill方法使requestAnimationFrame在chrome上工作,因为它是一种常见的解决方法。 The polyfill itself could be written in pure TypeScript with a bunch of casting, but the Javascript is clean and easy and you can remove it when Chrome removes the prefix without affecting your TypeScript. polyfill本身可以用纯TypeScript编写,并带有大量转换,但是Javascript简洁明了,您可以在Chrome删除前缀时将其删除,而不会影响您的TypeScript。

Note also that there is an issue currently with calling a named function expression within said function expression, which may bite you in code similar to the above. 还要注意,当前在所述函数表达式中调用命名函数表达式存在一个问题,这可能会在类似于上面的代码中咬住您。 Namely the below code will give you the error shown in the comments even though valid: 即,以下代码即使有效,也会给您注释中显示的错误:

function a() {
    var x = function b() {
        b(); // <-- "**The name 'b' does not exist in the current scope**"
    }
}

This is valid as per section 13 of the ECMAScript spec, but fails currently. 根据ECMAScript规范的第13节这是有效的,但当前失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM