Ash*_*war 1 javascript destructuring ecmascript-6
我想在es6中构造对象,但没有得到结果。
let animal ={
data:{
typee:{
title: "Cow",
legs:4
}
}
}
let {data:{typee:{title,legs}}}=animal;
Run Code Online (Sandbox Code Playgroud)
现在console.log(data)输出Error: data is not defined。我做错了什么?
销毁嵌套对象时,不会将中间值分配给const /变量。您必须明确分配它们:
const animal = {"data":{"typee":{"title":"Cow","legs":4}}};
const {
data, // assign the data
data: {
typee, // assign the typee
typee: {
title,
legs
}
}
} = animal;
console.log(data, typee, title, legs);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43 次 |
| 最近记录: |