javascript - ES6 解构为“this”
问题描述
尝试 ES6 解构。
我在 React 类构造函数中使用此代码:
let { class, ...rest } = props;
上面的代码有效。this.class
但我需要and中的变量this.rest
。
我有这个代码,它可以工作:
let { classes, ...rest } = props;
this.classes = classes;
this.rest = rest;
我正在寻找这样的东西:
{ this.classes, ...this.rest } = props;
解决方案
您可以使用重命名属性,但不幸的是它涉及到一些重复:
({ classes: this.classes, ...this.rest } = props);
classes
是我们要离开的属性的名称props
;this.classes
是分配给它的变量的名称。有了this.rest
,我们显然不需要命名原始属性名称。
演示:
function Foo(props) {
({ classes: this.classes, ...this.rest } = props);
}
const props = {
classes: 'some clases',
a: 'A',
b: 'B'
}
console.log(new Foo(props));
推荐阅读
- node.js - Node js如何连接读取文件excel云(在线)?
- css - (Next.js) 如何在 next/image 中更改 SVG 的颜色?
- java - 无法移交列表
(来自单 - >) 从 ViewModel 到 Activity。RecyclerView 保持为空。原因在哪里?
- css - CSS 的列数取决于大小
- r - R中按时间顺序排列的实验室数据
- javascript - Ajax 和 SweetAlert 不会等待确认继续
- java - File.getName 与 Path.getFileName
- django - Nginx connect() 连接到上游时失败(111:连接被拒绝),客户端:192.168.128.1,服务器:hello-1.local
- javascript - JavaScript 将平面数组转换为嵌套/分组和排序数组
- azureservicebus - 为什么消息锁过期后Azure Servicebus触发函数立即调用?