语法:const {} = variableName,谁能解释或指出我正确的方向 [英] Syntax: const {} = variableName, can anyone explain or point me into the right direction
问题描述
这个语法在 JavaScript 中是什么意思(可能是 ES6):
What does this syntax mean in JavaScript (ES6 probably):
const {} = 变量名;
const {} = variablename;
我目前正在尝试掌握 React.在很多例子中,我都遇到过这种语法.例如:
I'm currently trying to get a grip of React. In a lot of examples I came across that syntax. For example:
const {girls, guys, women, men} = state;
推荐答案
首先,这与 React 无关.它是 ECMAScript 6(或 JavaScript 2015,如果您愿意)的一部分.
First of all, this has nothing to do with React. It's part of ECMAScript 6 (or JavaScript 2015 if you prefer).
您在此处看到的内容称为解构赋值一个>:
What you see here is called Destructuring assignment:
const {girls, guys, women, men} = state;
// Is the same as
const girls = state.girls;
const guys = state.guys;
const women = state.women;
const men = state.men;
您在学习 React 时可能会遇到类似的情况:
You're probably going to encounter a similar patter while studying React:
import { methodA, methodB } from "my-module";
在这种情况下,您有一个名为 my-module
的模块,它公开了一些函数.使用 import {} from
语法,您可以选择要导入的函数.请注意,这不是解构赋值,尽管它以类似的方式工作.
In this case you have a module called my-module
that is exposing some functions. With the import {} from
syntax you choose which functions you want to import. Note that this is not destructuring assignment although it works in a similar way.
这篇关于语法:const {} = variableName,谁能解释或指出我正确的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!