TypeScript:使用RequireJS AMD的静态变量

时间:2013-08-24 16:30:18

标签: static requirejs typescript amd

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

示例代码:

App.ts:

import Game = require('Game');

var game = new Game();

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;

Grid.ts:

import Game = require('Game');

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

export = Grid;

在致电Game.Game之前检查this.Grid.SeeIfStaticWorks();表示已定义:

http://j.mp/1dCY8Ec

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

http://j.mp/1dCYISe

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


更新

从一个文件运行所有代码允许跨模块使用静态变量(demo here):

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()时未定义静态变量:

App.ts:

import Game = require('Game');

var game = Game.Game;

game.Grid.SeeIfStaticWorks();

Game.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;

Grid.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;

3 个答案:

答案 0 :(得分: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;
})();

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

答案 1 :(得分:1)

如果它可能对任何人有帮助,我已经设法在dojo框架内使用AMD创建静态变量,方法是将其声明包装起来'声明'功能(道场的本地功能,用于创建'类')。

(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);
});

}());

答案 2 :(得分: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(); 

Try it online.