最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

TypeScript error from valid JavaScript - Stack Overflow

programmeradmin6浏览0评论

The below code is valid JavaScript, yet in typescript it throws the following error. Any idea why?

Error 7 The name 'gameloop' does not exist in the current scope

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

EDIT: After first answer noticed copy and paste error that wasn't related to actual problem.

The below code is valid JavaScript, yet in typescript it throws the following error. Any idea why?

Error 7 The name 'gameloop' does not exist in the current scope

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

EDIT: After first answer noticed copy and paste error that wasn't related to actual problem.

Share Improve this question edited Oct 22, 2012 at 15:55 Fenton 252k73 gold badges402 silver badges410 bronze badges asked Oct 21, 2012 at 16:08 CiaránCiarán 7732 gold badges7 silver badges20 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

That's just a missing semicolon, preventing it to be correct both in Javascript and 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 plex.

In your case, the piler 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(). 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 plies down to this in 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. However, I got a runtime error, "window does not have requestAnimationFrame". This is because requestAnimationFrame is still prefixed in 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;

// 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 mon fix. 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.

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 ments 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.

发布评论

评论列表(0)

  1. 暂无评论