简体   繁体   English

Typescript 设置 Class 本身在方法中的值,Inheritance 从基础 Class

[英]Typescript Set Value of Class itself in Method, Inheritance from Base Class

How do I set a value of a class itself in its own method?如何以自己的方法设置 class 本身的值? Trying to utilize this .试图利用this Receiving an error below.收到以下错误。

export class ProductForm extends FormGroup {

    constructor(){
      super({
        productName: new FormControl()
      })
    }
   
    addMoreFieldsTest(): void {
      this = {
        productName: new FormControl(),
        productDescription: new FormControl()
     }
}

Error: The left-hand side of an assignment expression must be a variable or a property access.错误:赋值表达式的左侧必须是变量或属性访问。

I could use AddControl method, however want to set class itself for learning purposes.我可以使用 AddControl 方法,但是想设置 class 本身用于学习目的。

Updated Answer更新的答案

The controls of a FormControl do not reside directly on the FormGroup class but inside of a property on the class named controls FormControl 的控件不直接驻留在 FormGroup class 上,而是驻留在 class 上的属性内命名controls

Therefore, if you merely wish to add to the controls on the extending class you simply need to manipulate the controls property.因此,如果您只想添加到扩展 class 上的控件,您只需操作控件属性。

    export class ExtendedFormGroup extends FormGroup {
      constructor(
        controls: { [k: string]: AbstractControl },
        validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions,
        asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]
      ) {
        super({...controls, alwaysPresent: new FormControl()}, 
               validatorOrOpts, 
               asyncValidator
        );
        this.extendedProperties();
      }
    
      extendedProperties() {
        this.controls["additional_control"] = new FormControl("");
      }
    }

The above sample does two things上面的示例做了两件事

  • pass the constructor argument into super, and add an additional always present item on the control.将构造函数参数传递给 super,并在控件上添加一个额外的始终存在的项目。
  • Manipulate the controls property directly as reflected in your original question.如您的原始问题中所反映的那样直接操作controls属性。

Simply calling new ExtendedFormGroup({}) would now create a FormGroup with two pre-defined controllers alwaysPresent and additional_control现在只需调用new ExtendedFormGroup({})将创建一个带有两个预定义控制器alwaysPresentadditional_control的 FormGroup

Old response旧回应

Since JavaScript, and therefore TypeScript by extension implement classes in a way that are essentially just labelled blocks with prototypes on them, you can use the square bracket notation to access properties on the this scope of your class. Since JavaScript, and therefore TypeScript by extension implement classes in a way that are essentially just labelled blocks with prototypes on them, you can use the square bracket notation to access properties on the this scope of your class.

    class Parent {
      foo: string;
      constructor(foo: string) {
        this.foo = foo;
      }
    }
    
    class Child extends Parent {
      constructor(foo: string) {
        super(foo);
        this.assignPropertiesDirectly();
      }
    
      assignPropertiesDirectly() {
        this["bar"] = "Creates 'bar' on Child";
        this["foo"] = "overwrites foo with this text";
      }
    }

However, this approach is brittle as it not only completely defeats the purpose of using TypeScript in the first place, but it also relies on typing string values for property names, which at best will be a hassle to maintain, and at worst will be error prone.但是,这种方法很脆弱,因为它不仅完全违背了首先使用 TypeScript 的目的,而且还依赖于为属性名称键入字符串值,这充其量是维护起来很麻烦,最坏的情况是会出错易于。

It sounds to me like your problem may be a good candidate for composition design.在我看来,您的问题可能是作文设计的一个很好的候选者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM