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

前端 未结 2 1077
灰色年华
灰色年华 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 12:13

    Your workaround is the correct way to do this (at least from a pure Javascript perspective, not sure how Vue handles this). You can make it slightly shorter by using Object.assign:

    const { primaryNumber, typeOfExpression } = this.findPrimaryAndType(
        this.naturalExpressionYearOfBirth,
        this.gender,
    );
    Object.assign(this, { primaryNumber, typeOfExpression });
    

    Or, if you wanted to copy all the returned properties from findPrimaryAndType to the current instance, you could skip the destructuring altogether:

    const { naturalExpressionYearOfBirth, gender } = this;
    Object.assign(this, this.findPrimaryAndType(naturalExpressionYearOfBirth, gender));
    

提交回复
热议问题