var el1,el2 = [1,2] why not?
With destructuring (try in Firefox) you can do: let [param1, param2] = window.location.search.replace('?','').split('&')
And why: because what you're doing is defining param1 to be undefined and param2 to be the value of that expression, just like var a, b = 1 would result in a being undefined and b being 1.
This doesn't do what you want as assignments in initialisers are optional. This has the semantic effect of meaning that there is no difference between
var a, b = foo;
and
var a; var b = foo;
We're working on introducing destructuring assignment so you'll be able to do
var [a, b] = foo;
which is what you want, but we're not quite there yet.
ReferenceError: Invalid left-hand side in assignment on chrome
var a,b = 1; sets b to 1 and a is undefined, this is more C like semantics.
Where as a,b = b,a is more pythonis; Well then a,b=[1,2] should make sense ?
/me also agrees to var[a,b] = foo;
On Wed, Jul 4, 2012 at 10:31 AM, Hemanth H.M <hemanth.hm at gmail.com> wrote:
var a,b = 1; sets b to 1 and a is undefined, this is more C like semantics.
Where as a,b = b,a is more pythonis; Well then a,b=[1,2] should make sense ?
/me also agrees to var[a,b] = foo;
It might be helpful to read through the currently harmonized proposal for destructured assignments:
On Jul 4, 2012, at 10:31 AM, Hemanth H.M wrote:
var a,b = 1; sets b to 1 and a is undefined, this is more C like semantics.
Where as a,b = b,a is more pythonis; Well then a,b=[1,2] should make sense ?
No, because all of these things have defined semantics. Let's say you replace
var a,b=[1,2] with a,b=[1,2]
That is still valid today. It evaluates as resolving "a" (which may or may not have side effects), and then assigning [1,2] to b.
There is definitely code that does this in the variable declaration syntax, and knowing the web, probably code that does it in the non-var case. You can't use type checks either, because var a,b=c should not have different behaviour depending on the value of c.
It has to be a distinct syntax, which is what we have been developing -- see harmony:destructuring -- but that takes time. We have to resolve all of the potential issues in the syntax and semantics, then engines have to implement it.
This might be silly, but let the code speak :
var param1,param2 = window.location.search.replace('?','').split('&') a,b=b,a
To see how a JavaScript parser understands the semantic of that code, just paste it into esprima.org/demo/parse.html.
Thank you all, for your fantastic feedback!
Hello Hackers,
This might be silly, but let the code speak :
var param1,param2 = window.location.search.replace('?','').split('&') undefined param1 undefined param2 ["foo=bar", "hello=world"] a=1 1 b=2 2 a,b=b,a 1 a 1 b 2
Why not param1 be equal to "foo=bar" and param2 be equal to "hello=world"?