2013-08-24 4 views
0

TypeScript 0.9.1.1을 사용하면 다른 모듈/파일에서 정적 변수에 액세스 할 때 정의되지 않습니다.TypeScript : RequireJS에서 정적 변수 사용 AMD

예 번호 :

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 :

0,123,824,221,418

http://j.mp/1dCY8Ec

을하지만 SeeIfStaticWorks() 내부에서 액세스를 시도 할 때 정의되지 않는다 :

http://j.mp/1dCYISe

질문은 다음과 같습니다 this.Grid.SeeIfStaticWorks();를 호출하기 전에 Game.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; 

    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와 함께 :


업데이트 : 모듈에서 정적 변수 (demo here)를 사용하여 허용 하나 개의 파일에서 모든 코드를 실행

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이 그 때문에 파일 Game.tsGame.Game을 구문 분석됩니다

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; 

답변

1

설정되지 않았습니다. 당신은 생성 된 자바 스크립트 것을 볼 수 있습니다

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

당신이 정의의 지점을 설정해야합니다 뭔가를 내보낼 때 정적 변수를 사용할 수 있습니다려면 (안 느리게 귀하의 경우와 같은).그래서 :

싱글을 관리하는 방법에
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; 
})(); 

는 별도의 설명입니다 :

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

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

당신은 생성 된 자바 스크립트를 볼 수 있습니다. 하지만 위의 코드로 충분하다고 생각합니다. (진정한 싱글 톤을 원한다면 추가 검사를 할 수 있습니다).

+0

'SeeIfStaticWorks()'가'Game.Game'이 인스턴스화되기 전에 실행되기 때문에 그렇게 작동하지 않습니다. 내가 사용하는 것에 더 가까운 코드로 질문에 대한 업데이트를 게시했습니다. RequireJS와 관련된 문제가있는 것 같습니다. 좋은 AngularJs/TS 비디오 btw :) –

+0

그것은이 방법으로 작동하지 않으므로, 제발 대답을 삭제할 수 있습니까? 그러면 정답을 더 빨리 얻을 수 있습니다. –

+0

다른 답변보기 – basarat

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.

+1

죄송합니다. 무례하게 들리고 싶지는 않지만 RequireJS를 사용하지 않고 게임 및 그리드 모듈을 다른 파일로 사용하지 않습니다. 현재 나의 문제는 문제가 [RequireJS의 순환 참조] (http://requirejs.org/docs/api.html#circular)와 관련이 있지만 필요한 솔루션은 TypeScript의 키워드이므로 필요하지 않습니다. . –

1

, 나는에 의해 도장 프레임 워크 내에서 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); 
}); 

}());