Typescript全局变量

Typescript全局变量,typescript,Typescript,是否有一种方便的方法可以在模块内访问全局变量,而不会出现编译器错误,即下面使用的画布宽度 export class Bullet { x: number = 22; y: number = 22; constructor (speed: number) { this.xVelocity = speed; } inBounds() { return thi

是否有一种方便的方法可以在模块内访问全局变量,而不会出现编译器错误,即下面使用的画布宽度

    export class Bullet {


        x: number = 22;
        y: number = 22;

        constructor (speed: number) {
            this.xVelocity = speed;
        }

        inBounds() {
            return this.x >= 0 && this.x <= CANVAS_WIDTH &&
                this.y >= 0 && this.y <= CANVAS_HEIGHT;
        };
}
}
导出类项目符号{
x:数字=22;
y:数字=22;
建造师(速度:数字){
这个.xVelocity=速度;
}
内边界(){

返回this.x>=0&&this.x=0&&this.y这是一个精心设计的示例,但是您可以使用模块作用域来包含将从多个类中使用的变量,而不是尝试推送到全局作用域

module MyModule {
    var x: number = 5;

    export class FirstClass {
        doSomething() {
            x = 10;
        }
    }

    export class SecondClass {
        showSomething() {
            alert(x.toString());
        }
    }
}

var a = new MyModule.FirstClass();
a.doSomething();

var b = new MyModule.SecondClass();
b.showSomething();
关于使用同一变量的多件事情的所有常规规则都适用于这里——您不希望在调用代码上强制执行特定的事件顺序


汇编至:

var MyModule;
(function (MyModule) {
    var x = 5;

    var FirstClass = (function () {
        function FirstClass() {
        }
        FirstClass.prototype.doSomething = function () {
            x = 10;
        };
        return FirstClass;
    })();
    MyModule.FirstClass = FirstClass;

    var SecondClass = (function () {
        function SecondClass() {
        }
        SecondClass.prototype.showSomething = function () {
            alert(x.toString());
        };
        return SecondClass;
    })();
    MyModule.SecondClass = SecondClass;
})(MyModule || (MyModule = {}));

var a = new MyModule.FirstClass();
a.doSomething();

var b = new MyModule.SecondClass();
b.showSomething();
define(["require", "exports"], function(require, exports) {
    var Game = (function () {
        function Game(canvasElem) {
            Game.canvas = canvasElem;
            Game.CANVAS_WIDTH = Game.canvas.width();
            Game.CANVAS_HEIGHT = Game.canvas.height();
        }
        return Game;
    })();
    exports.Game = Game;

    var Bullet = (function () {
        function Bullet() {
            this.x = 22;
            this.y = 22;
        }
        Bullet.prototype.inBounds = function () {
            // accessing static properties
            return this.x >= 0 && this.x <= Game.CANVAS_WIDTH && this.y >= 0 && this.y <= Game.CANVAS_HEIGHT;
        };
        return Bullet;
    })();
    exports.Bullet = Bullet;
});
//# sourceMappingURL=dhdh.js.map

您需要将这些属性定义为静态,然后您可以像这样轻松地访问它

export class Game {
    static canvas: JQuery;
    static CANVAS_WIDTH: number;
    static CANVAS_HEIGHT: number;
    bullet: Bullet;

    constructor(canvasElem: JQuery) {
        Game.canvas = canvasElem;
        Game.CANVAS_WIDTH = Game.canvas.width();
        Game.CANVAS_HEIGHT = Game.canvas.height();
    }
}

export class Bullet {
    x: number = 22;
    y: number = 22;

    public inBounds() {
        // accessing static properties
        return this.x >= 0 && this.x <= Game.CANVAS_WIDTH && this.y >= 0 && this.y <= Game.CANVAS_HEIGHT;
    }
}
导出类游戏{
静态画布:JQuery;
静态画布宽度:数字;
静态画布高度:数字;
子弹:子弹;
构造函数(canvasElem:JQuery){
Game.canvas=画布;
Game.CANVAS_WIDTH=Game.CANVAS.WIDTH();
Game.CANVAS_HEIGHT=Game.CANVAS.HEIGHT();
}
}
出口级子弹头{
x:数字=22;
y:数字=22;
公共内边界(){
//访问静态属性
返回this.x>=0&&this.x=0&&this.y=0&&this.x=0&&this.y我不确定我是否想鼓励这样做,但要逐字回答OP的问题:

您可以将任何您想要的内容放入全局范围,然后在其他地方引用它。 例如,您可以在index.html等效项上添加以下内容:

function logit(x, loc) {
  if (console && console.log) {
    console.log(loc, JSON.stringify(x, null, '  '));
  }
}
现在,您可以在任何地方使用它,至少有两种方式

模糊你的全球范围。
(如有窗口).logit(项目,“某些位置”)

哎哟

使用声明 您也可以使用

因此,在常规类型脚本文件中:

declare const logit: (x: any, loc: string) => void;
// you could also just use
// declare const logit: any;

export default class MyClass {
  public handleIt(x: string) {
    logit(x, "MyClass.handleIt");
    // ... logic
  }
}
请注意,任何文件,而不仅仅是“index.html”,都是将内容推到全局范围的启动点。请记住,如果您(比如)懒惰地加载模块,您可能会遇到麻烦


你的榜样 因此,对于您来说,可以按照您的意愿全局设置这两个值(或者非TypeScript库可以为您这样做,这几乎可以使这成为一个有效的用例),然后执行以下操作

declare let CANVAS_WIDTH: number; // maybe const, I don't know your use case.
declare let CANVAS_HEIGHT: number;

export class Bullet {
  x: number = 22;
  y: number = 22;

  constructor (speed: number) {
    this.xVelocity = speed;
  }

  inBounds() {
    return this.x >= 0 && this.x <= CANVAS_WIDTH &&
    this.y >= 0 && this.y <= CANVAS_HEIGHT;
  };
}
declare let CANVAS\u WIDTH:number;//可能是const,我不知道您的用例。
声明高度:数字;
出口级子弹头{
x:数字=22;
y:数字=22;
建造师(速度:数字){
这个.xVelocity=速度;
}
内边界(){

返回this.x>=0&&this.x=0&&this.y,您将在游戏类中设置CANVAS\u WIDTH?的值,该类使用Bullet类GameObjects=module(“GameObjects”)导入模块你在游戏类中有画布宽度,需要在Bullet类中访问它。我说得对吗?这在最新版本的typescript中仍然有效吗?它似乎不存在示例编译…添加编译的js以进行比较添加编译的js文件