不能在构造函数中分配这个 [英] Can't assign this in constructor

查看:25
本文介绍了不能在构造函数中分配这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 values 中的道具合并到 this 中.

I'm trying to merge the props from values into this.

以下会引发错误.我该怎么做?

The following throws an error. How can I do this?

this = {...this, ...values}

推荐答案

您可以使用 Object.assign 方法:

You could extend this with Object.assign method:

class Foo {
  constructor (props) {
    Object.assign(this, props);
  }
}

const foo = new Foo({ a: 1 });
console.log(foo.a); // 1

这篇关于不能在构造函数中分配这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆