如果在Typescript类中使用诸如delete
和continue
作为方法名称的保留关键字,则IE8会出现问题。
例如:
class Foo {
delete() : void {
console.log('Delete called');
}
continue() : void {
console.log('Continue called');
}
}
生成以下javascript:
var Foo = (function () {
function Foo() {
}
Foo.prototype.delete = function () {
console.log('Delete called');
};
Foo.prototype.continue = function () {
console.log('Continue called');
};
return Foo;
})();
但是,这在IE8中效果不佳。IE8宁愿这样:
var Bar = (function () {
function Bar() {
}
Bar.prototype['delete'] = function () {
console.log('Delete called');
};
Bar.prototype['continue'] = function () {
console.log('Continue called');
};
return Bar;
})();
有什么办法可以保持Typescript的Typed优势,同时保持我的代码IE8正常运行?
在我的特殊情况下,我试图编写一个实现ng.IHttpService
接口的类,因此需要具有delete
作为实例方法的类。
class Foo {
"delete"() : void {
console.log('Delete called');
}
"continue"() : void {
console.log('Continue called');
}
}
转换为
var Foo = (function () {
function Foo() {
}
Foo.prototype["delete"] = function () {
console.log('Delete called');
};
Foo.prototype["continue"] = function () {
console.log('Continue called');
};
return Foo;
})();
请注意,我从未使用过TypeScript。我刚去他们的网站,快速浏览了PDF规范,发现可以使用字符串文字,并在TypeScript游乐场中对其进行了尝试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句