How can I use this in the left side of the object destructuring assignment?

前端 未结 2 1074
灰色年华
灰色年华 2021-01-15 11:29

(This question is not specific to Vue, but it is in a Vue project, that is why the strange use of the this in front of the functions and variables.)

I h

2条回答
  •  囚心锁ツ
    2021-01-15 11:58

    You can spell out the destructuring targets, instead of implicitly creating const variables:

    ({
      primaryNumber: this.primaryNumber,
      typeOfExpression: this.typeOfExpression
    } = this.findPrimaryAndType(
      this.naturalExpressionYearOfBirth,
      this.gender,
    ));
    

    Of course that's not very helpful in terms of conciseness. If those two properties are the only ones on the result object, you can however use Object.assign:

    Object.assign(this, this.findPrimaryAndType(
      this.naturalExpressionYearOfBirth,
      this.gender,
    ));
    

提交回复
热议问题