Here is my code:
class Animal {
constructor(public name: string){}
}
class Cat extends Animal {
constructor(public name: string) {
super(name);
}
}
It outputs the following code:
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);
And as you can see from this demo , the constructor
gets enumerated as a key.
The property constructor
of the Class Cat's prototype should be non-enumerable, but the method __extends has changed this. How can I repair this?
I know the following code can achieve this, but I want a way that TypeScript natively supports!
Object.defineProperty(Cat.prototype, 'constructor', {
enumerable: false
});
You need to define your own __extends
variable that uses Object.create
instead of new __()
to setup the prototype chain.
There is a feature request to allow you to do this at a global level: https://github.com/Microsoft/TypeScript/issues/1622
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.