语法:const {} = variableName,任何人都可以解释或指出我正确的方向 [英] Syntax: const {} = variableName, can anyone explain or point me into the right direction

查看:151
本文介绍了语法:const {} = variableName,任何人都可以解释或指出我正确的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种语法在JavaScript(ES6可能)中意味着什么:

What does this syntax mean in JavaScript (ES6 probably):

const {} = variablename;

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 {},您可以选择要导入的函数。请注意,这不是解构赋值,尽管它的工作方式类似。

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屋!

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