Tel*_*ion 3 javascript reactjs react-router
现在正在学习 React。我在 Router 的舞台上,在他们的文档中发现了一些我不明白的代码。(他们使用了很多短语法运算符和其他东西,所以很难用谷歌搜索或想出它的用途)。
所以这里是代码:
const { from } = this.props.location.state || { from: { pathname: "/" } };
const { redirectToReferrer } = this.state;
Run Code Online (Sandbox Code Playgroud)
在左侧声明“某物”时,它在 内部{ },为什么?
对于那些仍然对对象解构感到困惑的人,我可以举一个例子:
假设您有一个名为 car 的对象
const car = {
type: 'van',
model: 'honda',
...etc,
}
Run Code Online (Sandbox Code Playgroud)
然后,而不是像这样重复调用汽车对象内的一些变量:
const type = car.type;
const model = car.model;
Run Code Online (Sandbox Code Playgroud)
您可以使用解构对象并以更简单的方式编写它:
const { type, model } = car;
Run Code Online (Sandbox Code Playgroud)
它被称为对象解构。它是一个 JavaScript 表达式,允许我们从数组、对象、映射和集合中提取数据。请参阅以下链接了解更多详情。
https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/