简体   繁体   English

用函数参数破坏分配

[英]Destructuring assignment with function arguments

function foo( [a,b] ) {

    console.log(a);
    console.log(b);
}

foo( [12,34] );

Prints: 印刷品:

12
34

Also here: 也在这里:

 var { a:x, b:y } = { a:7, b:8 };
 console.log(x); // prints: 7
 console.log(y); // prints: 8

Is this method of assignment valid? 这种分配方法有效吗? Will this method bring any problems? 这种方法会带来任何问题吗?

Also using the same technique we can swap two variables: 同样使用相同的技术,我们可以交换两个变量:

var a = 1;
var b = 2;
[a,b] = [b,a];

I just wanted to know what problems will arise in future with this type of assignment? 我只是想知道这种作业将来会出现什么问题? Where can I find the best reference relating to this type of assignments? 在哪里可以找到与此类作业相关的最佳参考?

That is a Javascript 1.7 feature, it's part of the Mozilla implementation of Javascript. 这是Javascript 1.7功能,它是Mozilla Javascript实现的一部分。

https://developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.7#Destructuring_assignment_(Merge_into_own_page.2Fsection) https://developer.mozilla.org/zh-CN/docs/JavaScript/New_in_JavaScript/1.7#Destructuring_assignment_(Merge_into_own_page.2F部分)

It is not part of any ECMAScript standard (as far as I am aware) and I don't think there are any plans to make it available in other browsers; 据我所知,它不是任何ECMAScript标准的一部分,并且我认为没有任何计划使其在其他浏览器中可用。 you should not use this in portable websites. 您不应在便携式网站中使用此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM