2
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?

Maizere Pathak.Nepal
  • 2,383
  • 3
  • 27
  • 41
  • The first one gives me "unexpected token [", the second "unexpected token {" and the third "invalid left-hand side in assignment". Which browser (or other JS implementation) accepts those assignments? – JJJ Apr 12 '13 at 07:03
  • All of those are invalid code. How'd you get them to work? – Joseph Apr 12 '13 at 07:05
  • Since you won't tell how or where you got these to work and all the examples are clearly invalid JS, I'm going to have to vote to close as not a real question. – JJJ Apr 12 '13 at 07:17
  • @Juhana the article says it is a js 1.7 code – Maizere Pathak.Nepal Apr 12 '13 at 07:20
  • What article would that be? – JJJ Apr 12 '13 at 07:20
  • @Juhana Magical Javascript for Internet Explorer 22 – Ian Apr 12 '13 at 07:21
  • @juhana plz wait i will provide u a reference to it – Maizere Pathak.Nepal Apr 12 '13 at 07:21
  • @juhana i have no tecnic on how to point to exaact topic so if u like to close this post,plz proceed.the code didn't work as it was said on my browser too – Maizere Pathak.Nepal Apr 12 '13 at 07:29
  • @Maizere - You mean this part of the document? https://code.google.com/p/jslibs/wiki/JavascriptTips#Kind_of_destructuring_assignments (hover your mouse over the heading, right-click the P that appears to the right) – enhzflep Apr 12 '13 at 07:30
  • @juhana [here](http://code.google.com/p/jslibs/wiki/JavascriptTips#Destructuring_assignment_with_function_arguments). – Maizere Pathak.Nepal Apr 12 '13 at 07:34
  • Most browsers support JavaScript 1.5. The 1.7 features are available mostly in Firefox and even then only when you explicitly specify that the script is of version 1.7 (or higher). – JJJ Apr 12 '13 at 07:47
  • BTW, this seems like a duplicate of http://stackoverflow.com/questions/6983026/javascript-assign-array-values-to-multiple-variables – jacobq Feb 25 '14 at 15:38

1 Answers1

0

That is a Javascript 1.7 feature, it's part of the Mozilla implementation of Javascript.

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

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; you should not use this in portable websites.

drquicksilver
  • 1,627
  • 9
  • 12
  • Actually, isn't it part of ECMAScript6 / harmony? http://wiki.ecmascript.org/doku.php?id=harmony:destructuring – jacobq Feb 25 '14 at 15:30
  • 1
    From the January 20, 2014 working draft (rev 22) of the ES6 specification: "2.13.5 Destructuring Assignment Supplemental Syntax In certain circumstances when processing the production AssignmentExpression : LeftHandSideExpression = AssignmentExpression the following grammar is used to refine the interpretation of LeftHandSideExpression...." [(drafts)](http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts) – jacobq Feb 25 '14 at 15:36