typescript 打字稿派生类不能有相同的变量名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35708845/
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 03:19:06  来源:igfitidea点击:

typescript derived class cannot have the same variable name?

typescript

提问by Angel Angel

Why can't typescript derived class have the same variable name? Even these members are private. Is there an alternative to this, or am I doing something wrong?

为什么打字稿派生类不能具有相同的变量名?甚至这些成员都是私人的。有没有替代方法,或者我做错了什么?

class ClassTS {

    private nom: string = "ClaseTS";

    constructor() {

    }
}

class ClassTSDer extends ClassTS {

    private nom: string = "ClassTS";

    constructor() {
        super();
    }
}

I found this while practising with TS.

我在与 TS 一起练习时发现了这一点。

Class 'ClassTSDer' incorrectly extends base class 'ClaseTS'. Types have separate declarations of a private property 'nom'. ClassTSDer

class ClassTSDer

“ClassTSDer”类错误地扩展了基类“ClaseTS”。类型具有私有属性 'nom' 的单独声明。类TSDer

类类TSDer

you could, use protected; yes but if I do not want to use protected, would I have to use another name?

你可以,使用 protected;是的,但如果我不想使用受保护的,我是否必须使用另一个名称?

回答by Ryan Cavanaugh

The properties must have different names.

属性必须具有不同的名称。

Remember that at runtime, JavaScript class instances are just objects, and objects are just mappings from key to value. The property names are the key, and you can't have two different keys with the same name.

请记住,在运行时,JavaScript 类实例只是对象,而对象只是从键到值的映射。属性名称是键,您不能有两个具有相同名称的不同键。

回答by toskv

The properties must have different names.

属性必须具有不同的名称。

If you'll look at the generated ES5 code you can see that declaring a property on the child class with the same name as a private property as the parent will overwrite the parent one thus breaking encapsulation.

如果您查看生成的 ES5 代码,您会发现在子类上声明一个与父类同名的私有属性的属性将覆盖父类,从而破坏封装。

/**
 * ClassTS
 */
var ClassTS = (function () {
    function ClassTS() {
        this.nom = "ClaseTS";
    }
    ClassTS.prototype.someMethod = function () {
        console.log(this.nom);
    };
    return ClassTS;
}());
/**
 * ClassTSDer
 */
var ClassTSDer = (function (_super) {
    __extends(ClassTSDer, _super);
    function ClassTSDer() {
        _super.call(this);
        this.nom = "ClassTS";
    }
    ClassTSDer.prototype.childMethod = function () {
        _super.prototype.someMethod.call(this);
    };
    return ClassTSDer;
}(ClassTS));

In this case for any function from the parent called in the child will result in this.nomhaving the value "ClassTS" instead of "ClaseTs" as you'd expect from a private property.

在这种情况下,对于来自父级在子级中调用的任何函数,都会导致this.nom具有值“ClassTS”而不是“ClaseTs”,正如您对私有属性所期望的那样。

The compiler does not complain about the protectedproperties (even though they generate the same ES5 code) because the expectation of encapsulation is no longer there.

编译器不会抱怨受保护的属性(即使它们生成相同的 ES5 代码),因为不再存在对封装的期望。