0

使用 TypeScript 0.9.1.1,当尝试从另一个模块/文件访问静态变量时,它是未定义的。

示例代码:

应用程序.ts:

import Game = require('Game');

var game = new Game();

游戏.ts:

import Grid = require('Grid');

class Game
{
    public Grid: Grid;
    public static Game: Game;

    constructor()
    {
        Game.Game = this;
        this.Grid = new Grid();
        this.Grid.SeeIfStaticWorks();
    }
}

export = Game;

网格.ts:

import Game = require('Game');

class Grid
{
    public SeeIfStaticWorks()
    {
        var shouldNotBeUndefined = Game.Game;
    }
}

export = Grid;

Game.Game调用前检查this.Grid.SeeIfStaticWorks();显示它已定义:

但是当试图从内部访问它时,SeeIfStaticWorks()它是未定义的:

问题是:如何能够从其他模块访问静态变量?


更新:

从一个文件运行所有代码允许跨模块使用静态变量(此处为演示):

class Grid
{
    public SeeIfStaticWorks()
    {
        console.log(Game.Game);
        if (Game.Game)
            alert('Instance is defined!');
        else
            alert('Instance is undefined!');
    }
}

class Game
{
    public Grid: Grid;

    private static game : Game;
    public static get Game() : Game
    {
        if (this.game == null)
        {
            this.game = new Game();
        }

        return this.game;
    }

    constructor()
    {
        this.Grid = new Grid();
    }
}

var game = Game.Game;

game.Grid.SeeIfStaticWorks();

如果我对 AMD RequireJS 使用相同的逻辑,则调用时静态变量未定义SeeIfStaticWorks()

应用程序.ts:

import Game = require('Game');

var game = Game.Game;

game.Grid.SeeIfStaticWorks();

游戏.ts:

import Grid = require('Grid');

class Game
{
    public Grid: Grid;

    private static game : Game;
    public static get Game() : Game
    {
        if (this.game == null)
        {
            this.game = new Game();
        }

        return this.game;
    }

    constructor()
    {
        this.Grid = new Grid();
    }
}

export = Game;

网格.ts:

import Game = require('Game');

class Grid
{
    public SeeIfStaticWorks()
    {
        console.log(Game.Game);
        if (Game.Game)
            alert('Instance is defined!');
        else
            alert('Instance is undefined!');
    }
}

export = Grid;
4

3 回答 3

1

如果它可以帮助任何人,我已经设法在 dojo 框架中使用 AMD 创建静态变量,方法是包装它的“declare”函数(dojo 用于创建“类”的本机函数)。

(function () {
'use strict';

define([
    'dojo/dom',
    'dojo/_base/lang',
    'dojo/_base/declare'
], function (dom, lang, declare) {


    var constants = {
        SOME_CONSTANT: 'Here is it!'
    };

    var Car = declare(null, {
        constructor: function() {

        },

        throttle: function() {
            console.log('Vrrr!');
        }
    });

    lang.mixin(Car, constants);
    return Car;

});

}());

在客户端:

(function () {
'use strict';
define([
    'model/Car',
    'dojo/domReady!'
], function (Car) {
    var c = new Car();
    c.throttle();
    console.log(Car.SOME_CONSTANT);
});

}());

于 2014-12-03T17:51:03.130 回答
1

Game.ts这是因为解析文件时Game.Game没有设置。您可以在生成的 javascript 中看到:

var Game = (function () {
    function Game() {
        Game.Game = this;
        this.Grid = new Grid();
        this.Grid.SeeIfStaticWorks();
    }
    return Game;
})();

要在导出某些内容时使用静态变量,您必须在定义时设置它们(而不是像您的情况那样懒惰)。所以 :

class Game
{
    public Grid: Grid;
    public static Game: Game = new Game(); // set it outside the constructor

    constructor()
    {
        this.Grid = new Grid();
        this.Grid.SeeIfStaticWorks();
    }
}

你可以看到生成的javascript:

var Game = (function () {
    function Game() {
        this.Grid = new Grid();
        this.Grid.SeeIfStaticWorks();
    }
    Game.Game = new Game(); // Now this statement will execute when this javascript is parsed
    return Game;
})();

如何管理单例是一个单独的讨论。但我相信上面的代码就足够了。(如果你想要一个真正的单例,你可以进行额外的检查)。

于 2013-08-24T23:25:45.327 回答
0

以下与订单无关的代码有效。网格之前的演示游戏:

class Game
{
    public Grid: Grid;
    public static Game;

    constructor()
    {
        Game.Game = this;
        this.Grid = new Grid();
        this.Grid.SeeIfStaticWorks();
    }
}
class Grid
{
    public SeeIfStaticWorks()
    {
        console.log(Game.Game);
        if (Game.Game)
            alert('Instance is defined!');
        else
            alert('Instance is undefined!');
    }
}
// Finally 
var daGame = new Game(); 

或赛前网格:

class Grid
{
    public SeeIfStaticWorks()
    {
        console.log(Game.Game);
        if (Game.Game)
            alert('Instance is defined!');
        else
            alert('Instance is undefined!');
    }
}
class Game
{
    public Grid: Grid;
    public static Game;

    constructor()
    {
        Game.Game = this;
        this.Grid = new Grid();
        this.Grid.SeeIfStaticWorks();
    }
}
// Finally 
var daGame = new Game(); 

在线尝试。

于 2013-08-31T23:22:21.087 回答