Baj*_*lor 6 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.create(b) : (__.prototype = b.prototype, new __());
};
Run Code Online (Sandbox Code Playgroud)
它看起来与应该生成的完全一样.但问题是执行时Firefox会给出以下消息:
TypeError: b is undefined
Run Code Online (Sandbox Code Playgroud)
在Chrome中,错误看起来有点不同,但似乎是相同的来源:
Uncaught TypeError: Cannot read property 'prototype' of undefined
Run Code Online (Sandbox Code Playgroud)
打字稿中的实现看起来像这样
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了各种编译代码的方法,但结果总是一样的
要摆脱错误,你必须Medium在其中一个之前放置类声明Movie.
请注意,生成的js代码不仅仅是函数定义.它是函数和变量.这一切都有所不同.因为你有声明和表达式.关于这个问题的更多内容以及为什么js顺序中的表达式确实很重要,您可以阅读这篇优秀文章:JavaScript函数声明和评估顺序
| 归档时间: |
|
| 查看次数: |
7183 次 |
| 最近记录: |