我在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有一个错误。我的上面的代码段出了什么问题?
就是这样,谢谢。我没有在app.js中引用Thing.js和BMW.js。现在,TypeScript编译器在我的项目文件中配置如下:'"%(fullpath )"','')“IgnoreExitCode =”true“/>。我是否将整个事件更改为tsc --out app.js app.ts? –
传递编译器选项-out scripts \ app.js scripts \ app。我看到一个包含所有代码的单个Javascript文件。但是,如果我所引用的是app.js,我仍然可以获取实体undefined ... –
不知道以前发生了什么。选项1现在适合我。再次感谢。 –