子类的构造函数' prototype可以在TypeScript中枚举

时间:2015-06-06 03:58:19

标签: javascript typescript

这是我的代码

class Animal {
    constructor(public name: string){}
}
class Cat extends Animal {
    constructor(public name: string) {
        super(name);
    }
}

它输出以下代码:

var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var Animal = (function () {
    function Animal(name) {
        this.name = name;
    }
    return Animal;
})();
var Cat = (function (_super) {
    __extends(Cat, _super);
    function Cat(name) {
        _super.call(this, name);
        this.name = name;
    }
    return Cat;
})(Animal);

Class Cat的原型的属性构造函数默认是不可枚举的,但 __ extends 方法已经改变了这个,我该如何修复它? 我知道以下代码可以实现这一点,但我想要一种TypeScript原生支持的方式!

Object.defineProperty(Cat.prototype, 'constructor', {
    enumerable: false
});

1 个答案:

答案 0 :(得分:1)

您需要定义自己的__extends变量,该变量使用Object.create而不是new __()来设置原型链。

有一项功能请求可让您在全球范围内执行此操作:https://github.com/Microsoft/TypeScript/issues/1622