简体   繁体   English

打字稿:私人成员突然未定义

[英]Typescript: private member is suddenly undefined

So, I've got a basic Typescript application, that shouldn't actually cause any major problems, but it seems, that something's going wrong here and I have no idea what. 因此,我有一个基本的Typescript应用程序,它实际上不会引起任何重大问题,但看来这里出了点问题,我也不知道是什么。

I do have this private member minUpdateRate in my GameContainer class, that is initialized in the constructor. 我的GameContainer类中确实有此私有成员minUpdateRate ,该类在构造函数中初始化。 This seems to go well, because when GameContainer.start() is called, the console.log() method will print out 1 . 这样做似乎很顺利,因为调用GameContainer.start()时, console.log()方法将输出1

However, when the GameContainer.render() method is called, it seems to be out of the scope or something, the log method outputs undefined there. 但是,当GameContainer.render()方法时,似乎超出了范围之类,log方法在那里输出undefined

I'm pretty new to TypeScript and not that deep into JavaScript either (especially when it comes to scopes, it's getting confusing to me :/). 我对TypeScript还是很陌生,对JavaScript也没有那么深入(特别是在范围方面,它使我感到困惑:/)。 How can I however solve this? 但是,我该如何解决呢?

Main class: 主班:

class TwoDGame extends Game {
    public static main(context:CanvasRenderingContext2D) {
        var game:Game = new TwoDGame();
        var container:GameContainer = new GameContainer(context, game);

        container.start();

        return game;
    }
}

Game Container class: 游戏容器类:

class GameContainer {
    ...
    private minUpdateRate:number;
    private game:Game;
    private time:number;
    ...

    constructor(context:CanvasRenderingContext2D, game:Game) {
        ...
        this.minUpdateRate = 1;
        this.game = game;
    }

    public start() {
        ...
        console.log(this.minUpdateRate);
    }

    public render() {
        var now:number = new Date().getMilliseconds();
        var delta:number = now - this.time;
        console.log(this.minUpdateRate);

        if (delta > this.minUpdateRate) {
            ...
        }
    }
}

Render is called via setInterval in the script area: 通过脚本区域中的setInterval调用渲染:

var game = TwoDGame.main(context);

setInterval(game.getGameContainer().render, 16);

You're losing the object context when you pass setInterval() a reference to the "render" method that way. 当您通过setInterval()传递对“ render”方法的引用时,您将失去对象上下文。

setInterval(function() { game.getGameContainer().render(); }, 16);

There's no intrinsic relationship between a function and an object that refers to it via a property value. 函数和通过属性值引用它的对象之间没有内在联系。 By using an anonymous function, you can ensure that the "container" object will be the value of this inside "render". 通过使用匿名函数,可以确保“容器”对象将是“渲染”中this对象的值。

You could also do this with the .bind() method of the Function prototype: 您也可以使用Function原型的.bind()方法执行此操作:

setInterval(game.getGameContainer().render.bind(game.getGameContainer()), 16);

but that seems a little ugly in this case. 但这在这种情况下似乎有点丑陋。

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

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