【发布时间】:2017-12-12 00:33:40
【问题描述】:
是否可以通过使用另一个解构参数作为键从传入的对象中获取解构变量?
var test = {
a: {
b: 'c'
},
access: 'b'
};
myFunc(test);
function myFunc( { a : { access /*???*/ } } ) {
console.log(/*???*/); // should output 'c'
}
工作方式-
function myFunc( { a, access }) {
console.log(a[access]); // should output 'c'
}
【问题讨论】:
-
您可能打算声明:
function myFunc (c) {...并调用函数:myFunc(test.a.b); -
顺便说一句,它被称为破坏ur而不是破坏。
-
不,你不能简单地通过一次解构来做到这一点。
-
@alfasin 检查历史记录,在我发表评论之前就已经存在。 stackoverflow.com/revisions/47763647/3 --- 它是在 30 分钟前添加的。
-
我明白了,可能我的视图没有刷新...
标签: javascript ecmascript-6 destructuring