除了其属性之外,还为变形对象创建变量 [英] Creating variables for destructured objects in addition to their properties
本文介绍了除了其属性之外,还为变形对象创建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有:
const { state: { mode } } = this
console.log(mode) //'mode'
console.log(state) //undefined
我也想声明state
变量.
是否有一种方法可以在不将其分解为两个语句的情况下对其进行结构分解?
Is there a way to destructure this without breaking it into two statements?
const { state } = this
const { mode } = state
推荐答案
当然,只需使用逗号,就好像您要破坏父对象的另一个属性一样:
Sure, just use a comma as if you were destructing another property of the parent object:
const obj = { state: { mode: 'str' }};
const { state: { mode }, state } = obj;
console.log(mode);
console.log(state);
请注意,这看起来非常相似,但是并不相同如下所示的import
语法:
Note that this looks very similar to, but is not the same as the following import
syntax you may have seen:
import React, { Component } from 'react'
在这里,方括号中的变量为命名为exports ,而普通变量是默认的export,与嵌套对象完全不同.
Here, the variables in brackets are named exports, while the plain variable is the default export, which is entirely different from nested objects.
这篇关于除了其属性之外,还为变形对象创建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文