2012-10-13 102 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

感謝您的回覆。 –