2012-10-13 103 views
2

我在BMW.ts定义的命名BMW类,如下所示:打字稿内部模块使用

///<reference path="../Thing.ts"/> 

module Entities.Cars { 

    import e = Entities; 

    export class BMW extends Vehicle { 

     public series: string; 

     constructor (model : string, series : string) { 
      super("BMW", model) 
      this.series = series; 
     } 

     drive() { 
      alert("driving a bimmer is a different kind of feeling"); 
     }  

     toString() : string 
     { 
      return this.getName() + " " + this.series + " " + this.getType(); 
     } 
    } 
} 

在另一文件Thing.ts,我有限定车辆与物类,如下所示:

module Entities { 

    // Class 
    export class Thing { 

     private _name: string; 
     private _type: string; 

     // Constructor 
     constructor (public name: string, public type: string) { 
      this._name = name; 
      this._type = type; 
     } 

     getName(): string { return this._name; } 
     setName(name: string) { this._name = name; } 


     getType(): string { return this._type; } 
     setType(name: string) { 
      this._type = name; 
     } 

     toString() : string 
     { 
      return "Entities.Thing";   
     } 
    } 

    export class Vehicle extends Thing { 

     public cargoCapacity: number; 
     public fuelType: string; 
     public owner: string; 

     constructor (make: string, model : string) { 
      super(make, model) 
     } 

     drive() { 
     } 

     toString(): string { 
      return "Entities.Vehicle"; 
     } 
    } 
} 

当我试图引用的事情和宝马打字稿文件后执行以下代码:

var car = new Entities.Cars.BMW("335i", "E90"); 
car.drive(); 

我得到一个异常以下错误“Microsoft JScript运行时错误:无法获取属性'BMW'的值:对象为null或未定义”。为宝马生成的Javascript有一个错误。我的上面的代码段出了什么问题?

回答

6

你的代码没有问题,所以它看起来像你生成的javascript文件的导入顺序是错误的。该规范指出以下几点:

///<reference path='Things.ts'/> 
///<reference path='bmw/BMW.ts'/> 
var car = new Entities.Cars.BMW("335i", "E90"); 
car.drive(); 

在这一点上,你有两种选择:

  1. 让编译器确定正确的

    Initialization order of the source files that make up the global module ultimately depends on the order in which the generated JavaScript files are loaded at run-time (which, for example, may be controlled by tags that reference the generated JavaScript files).

    如下我已经生成的文件app.ts通过生成单个输出文件执行文件的顺序

    tsc --out app.js app.ts

    那么你只需要源app.js

  2. 手动指定正确的订单。对我而言,以下是唯一没有抛出错误的顺序。

    <html> 
        <head> 
         <script src="Things.js"></script> 
         <script src="bmw/BMW.js"></script> 
         <script src="app.js"></script> 
        </head> 
        <body> 
        </body> 
    </html> 
    
+1

就是这样,谢谢。我没有在app.js中引用Thing.js和BMW.js。现在,TypeScript编译器在我的项目文件中配置如下:'"%(fullpath )"','')“IgnoreExitCode =”true“/>。我是否将整个事件更改为tsc --out app.js app.ts? –

+0

传递编译器选项-out scripts \ app.js scripts \ app。我看到一个包含所有代码的单个Javascript文件。但是,如果我所引用的是app.js,我仍然可以获取实体undefined ... –

+0

不知道以前发生了什么。选项1现在适合我。再次感谢。 –

0

你的代码没问题。

我的猜测是,你没有正确地将脚本标签放在你的head元素中(错误的顺序,或者省略了一些)。

解决这个问题最简单的方法,而不必记住正确的声明顺序是通过设置--out选项使用tsc编译器的单个.js输出文件。

编辑:根据您正在使用哪个js场景(WSH,web应用程序或其他js环境),您需要以不同的方式链接js源文件。 以wsh为例,您可以使用FileSystemObject读取源文件,然后对其进行评估。 或者你可以使用AMD ...

+0

感谢您的回复。 –