在下面的函数中,我获得带有属性的textarea对象current。
current
此处,嵌套解构使用Start和End变量。但是current变量不起作用。
Start
End
function someFunction({ current: { selectionStart: Start, selectionEnd: End } }, AppStateSetter) { // 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 }
您可以在Babel编译器上对其进行测试:
这段代码:
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未创建变量。