我在 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 中,我将 Vehicle 和 Thing 类定义如下:
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";
}
}
}
当我在引用 Thing 和 BMW TypeScript 文件后尝试执行以下代码时:
var car = new Entities.Cars.BMW("335i", "E90");
car.drive();
我收到以下错误“Microsoft JScript 运行时错误:无法获取属性‘BMW’的值:对象为空或未定义”的异常。为 BMW 生成的 Javascript 有错误。我上面的代码片段有什么问题?