Javascript object bracket notation ({ Navigation } =) on left side of assign(赋值左侧的 Javascript 对象括号表示法 ({ Navigation } =))
问题描述
我以前没有见过这种语法,我想知道它是怎么回事.
I haven't seen this syntax before and am wondering what it's all about.
var { Navigation } = require('react-router');
左边的括号抛出语法错误:
The brackets on the left are throwing a syntax error:
意外的令牌{
我不确定 webpack 配置的哪一部分正在转换或语法的目的是什么.是和谐的东西吗?有人可以启发我吗?
I'm not sure what part of the webpack config is transforming or what the purpose of the syntax is. Is it a Harmony thing? Can someone enlighten me?
推荐答案
叫做 解构赋值,它是ES2015标准的一部分.
It's called destructuring assignment and it's part of the ES2015 standard.
解构赋值语法是一个 JavaScript 表达式可以使用 a 从数组或对象中提取数据反映数组和对象字面量构造的语法.
The destructuring assignment syntax is a JavaScript expression that makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals.
来源: 解构赋值参考在 MDN 上
对象解构
var o = {p: 42, q: true};
var {p, q} = o;
console.log(p); // 42
console.log(q); // true
// Assign new variable names
var {p: foo, q: bar} = o;
console.log(foo); // 42
console.log(bar); // true
数组解构
var foo = ["one", "two", "three"];
// without destructuring
var one = foo[0];
var two = foo[1];
var three = foo[2];
// with destructuring
var [one, two, three] = foo;
这篇关于赋值左侧的 Javascript 对象括号表示法 ({ Navigation } =)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:赋值左侧的 Javascript 对象括号表示法 ({ Navigation } =)


- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01