Assigning to empty array/object literals
On 10.08.2010 9:21, Michael Day wrote:
Hi,
Firefox 3.6.8 supports the following expressions:
[] = 3;
and:
{} = 3;
Although this object-literal version will trigger a syntax error at block scope, and so needs to be written like this:
x = ({} = 3);
In any case, shouldn't both of these expressions be early syntax errors, as the left hand side can never be a reference?
For comparison, non-empty array and object literals throw an error:
[1] = 3; // SyntaxError x = ({foo: 1} = 3); // SyntaxError
Bug in Firefox? Or subtlety of the spec that's gone over my head? :)
Destructuring (or, regarding JS -- "Non-strict pattern matching").
[a] = [3];
a; // 3
{a: b} = {a: 3, x: 4};
b; // 3
{a: c} = {};
c; // undefined
[x, y] = {not_an_array: 42}; // OK
[x, [y, z]] = {nor_here: 99}; // TypeError
Destructuring (or, regarding JS -- "Non-strict pattern matching").
Thanks, I was fooled by the fact the left hand side was empty :)
Firefox 3.6.8 supports the following expressions:
[] = 3;
and:
{} = 3;
Although this object-literal version will trigger a syntax error at block scope, and so needs to be written like this:
x = ({} = 3);
In any case, shouldn't both of these expressions be early syntax errors, as the left hand side can never be a reference?
For comparison, non-empty array and object literals throw an error:
[1] = 3; // SyntaxError x = ({foo: 1} = 3); // SyntaxError
Bug in Firefox? Or subtlety of the spec that's gone over my head? :)
Best ,
Michael