typescript 打字稿覆盖构造函数中的扩展属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27696982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:57:10  来源:igfitidea点击:

Typescript overriding extended property within constructor

javascripttypescript

提问by Brent

I'm having an issue with Typescript where I extend a class and override a property from the super, however the super class property is still read in the constructor when I instantiate the sub class. Please see the below example:

我在使用 Typescript 时遇到了问题,我扩展了一个类并覆盖了 super 中的一个属性,但是当我实例化子类时,仍然在构造函数中读取了超类属性。请看下面的例子:

class Person {

    public type:string = 'Generic Person';

    public constructor() {
        console.log(this.type);
    }

}

class Clown extends Person {

    public type:string = 'Scary Clown';

}

var person = new Person(), // 'Generic Person'
    clown = new Clown(); // 'Generic Person'

console.log(person.type); // 'Generic Person'
console.log(clown.type); // 'Scary Clown'

My expected behaviour would be 'Scary Clown' when I instantiate an instance of Clown. Is there another way I can achieve this without passing the values into the constructor itself or having some sort of init method that I fire manually after instantiating?

当我实例化 Clown 的实例时,我的预期行为将是“可怕的小丑”。有没有另一种方法可以实现这一点,而无需将值传递给构造函数本身或在实例化后手动触发某种初始化方法?

Thanks in advance :)

提前致谢 :)

采纳答案by basarat

Property initializers are inserted right at the top of the constructor beforethe manually entered body of the constructor. So

属性初始值设定项插入到构造函数的顶部,在手动输入的构造函数主体之前。所以

class Person {
    public type:string = 'Generic Person';
    public constructor() {
        console.log(this.type);
    }
}

Becomes

成为

var Person = (function () {
    function Person() {
        this.type = 'Generic Person';
        // NOTE: You want a different value for `type`
        console.log(this.type);
    }
    return Person;
})();

As you can see there is no wayto get a different typein the parent constructor body using a property initializer.

如您所见,无法type使用属性初始值设定项在父构造函数体中获得不同的结果。

Alternatively don't use typeand rely on built-in constructorproperty:

或者不要使用type和依赖内置constructor属性:

interface Function{name?:string;}

class Person {    
    public constructor() {
        console.log(this.constructor.name);
    }
}

class Clown extends Person {    
}

var person = new Person(), // 'Person'
    clown = new Clown(); // 'Clown'

console.log(person.constructor.name); // 'Person'
console.log(clown.constructor.name); // 'Clown'

回答by Alex Barannikov

you need to set default values for properties inside constructor, like this:

您需要为构造函数中的属性设置默认值,如下所示:

class Person {
    type:string = 'Generic Person';
    constructor() {
        console.log(this.type);
    }
}
class Clown extends Person {
    constructor() {
        this.type = 'Scary Clown';
        super();
    }
}