Javascript TypeScript:TypeError b未定义
当我尝试在TypeScript中创建继承时,会生成以下JavaScript:Javascript TypeScript:TypeError b未定义,javascript,web,typescript,visual-studio-2015,Javascript,Web,Typescript,Visual Studio 2015,当我尝试在TypeScript中创建继承时,会生成以下JavaScript: var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
看起来和应该生成的完全一样。但问题是,Firefox在执行时给出了以下信息:
TypeError: b is undefined
在Chrome中,错误看起来有点不同,但似乎来源相同:
Uncaught TypeError: Cannot read property 'prototype' of undefined
typescript中的实现如下所示
class Movie extends Medium {
//Some Fields
constructor(title: string, description: string, ageRestriction: AgeRestriction, isBluRay: boolean) {
super(title, description, ageRestriction);
this.isBluRay = isBluRay;
}
}
class Medium implements IMedium {
//Getters, Setters and Fields
constructor(title: string, description: string, ageRestriction: AgeRestriction) {
this.title = title;
this.description = description;
this.ageRestriction = ageRestriction;
}
}
我已经尝试过各种编译代码的方法,但结果总是一样的为了消除错误,您必须在
电影
之前添加媒体
类声明
请注意,生成的js代码不仅仅是函数定义。它是函数和变量。那就完全不同了。因为您同时拥有声明和表达式。关于这个问题的更多信息,以及为什么js顺序的表达式很重要,你可以在这篇优秀的文章中阅读: