下面的函数接收一个对象,该对象具有属性 current
,它也是一个对象,并且它具有 selectionStart
和 selectionEnd
属性。
在这里,嵌套解构按预期使用 Start
和 End
变量工作,但我还需要 current
的值。
function someFunction({ current: { selectionStart: Start, selectionEnd: End } }) {
// do something with current, Start, and End
}
我如何使用解构得到它?
最佳答案
第一个解构只创建Start
和End
变量。如果要将 current
创建为变量,则需要再次声明它。
function ({ current: { selectionStart: Start, selectionEnd: End }, current }, AppStateSetter) {
// do something with current , Start , and End
}
您可以 test it on the Babel compiler :
这段代码:
const object = {
current: {
selectionStart: "prop 1",
selectionEnd: "prop2"
}
}
const { current: { selectionStart: Start, selectionEnd: End } } = object;
被传输到:
var object = {
current: {
selectionStart: "prop 1",
selectionEnd: "prop2"
}
};
var _object$current = object.current,
Start = _object$current.selectionStart,
End = _object$current.selectionEnd;
如您所见,未创建 current
变量。
关于javascript - 解构嵌套对象 : How to get parent and its children values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54591307/