最近,我发现,这种语法在JavaScript(铬53)的工作原理:命名数组元素
function foo([param1]) { // Function argument is declared as array and param1 is used as variable? What is the name of this syntax?
console.log(param1);
}
foo(['TestParameter1']); // Case 1 - works. Output: TestParameter1
foo('TestParameter1'); // Case 2 - works??? Why? Output: TestParameter1
foo(123); // Case 3 - does not work - VM860:1 Uncaught TypeError: undefined is not a function(…)
Result => TestParameter1 // this is the result
我看到的param1可以用作变量,在第一个参数索引为0引用项(声明为数组)。
我的问题是:
1)此语法如何命名(在[参数1]的部分,让您使用参数1可变)?
2)为什么“情况2”有效?有没有自动转换?
'item1'是如何定义的? – Redu
@Redu它在示例中定义如下:function foo([param1]){} –
[Destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment )。 – Xufox