Assignment

25 views
Skip to first unread message

John J Barton

unread,
Feb 26, 2013, 11:48:39 AM2/26/13
to js-t...@googlegroups.com
I'm hoping someone here can help me understand assignment in JS.

Intuitively assignment requires a reference on the LHS and most JS expressions result in values. So my first guess is that the only valid assignments look like:
   identifier = expr;
   expr.identifier = expr;
   expr[expr] = expr;
So we can assign to variables, object properties, and array elements.

But the ES documentation has lots of other LHS stuff ( in the ES6 draft, LeftHandSideExpression includes NewExpression and CallExpression)
so perhaps I am completely wrong.

Any hints?
jjb

Yusuke SUZUKI

unread,
Feb 26, 2013, 12:08:44 PM2/26/13
to js-t...@googlegroups.com
Hello,

This is because [[Call]] can return reference.

Seeing ECMA262 5.1th section 8.6.2, [[Call]] internal method can return reference.
This is the reason why LeftHandSideExpression includes NewExpression and CallExpression.

According to Allen, this is introduced to explain legacy IE behavior.

But this behavior makes spec bug, see

As the result, I think this feature will be removed in ES6.

I also think the preferable way to fix it would be to remove the [[Call]] returning Reference feature.

This discussion at Esprima thread is also interesting for you. https://code.google.com/p/esprima/issues/detail?id=81


--
--
http://clausreinke.github.com/js-tools/resources.html - tool information
http://groups.google.com/group/js-tools - mailing list information
 
---
You received this message because you are subscribed to the Google Groups "js-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to js-tools+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Regards,
Yusuke Suzuki

Peter van der Zee

unread,
Feb 26, 2013, 12:27:26 PM2/26/13
to js-t...@googlegroups.com
On Tue, Feb 26, 2013 at 5:48 PM, John J Barton
<johnj...@johnjbarton.com> wrote:
> But the ES documentation has lots of other LHS stuff ( in the ES6 draft,
> LeftHandSideExpression includes NewExpression and CallExpression)
> so perhaps I am completely wrong.

I think it's technically also valid to assign to a call in ES5
(foo()=5 see http://esparser.qfox.nl/#runnow:on,code:foo%28%29%3D5 ),
though I've yet to see that used in the wild.

ES6 will introduce various ways of destructuring.

Arrays:

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

And I believe object destructuring as well, though I've not kept up
much. Would look something like this:

var {a,b} = {a:1, b:2};

I'm sure somebody will chime in with more details :)

Personally, I'm not sure whether I'm happy with these changes. On the
one hand it's interesting and obviously useful. On the other hand I
worry that it'll lead to code that's hard to read and rather
inefficient (in terms of object garbage collection), though engines
will surely optimize for the obvious cases. It's the legibility I
worry about most. Anyhoo...

- peter

johnjbarton

unread,
Feb 26, 2013, 8:27:15 PM2/26/13
to js-t...@googlegroups.com, ma...@qfox.nl
Thanks Peter and Yusuke, very helpful.
jjb

Ariya Hidayat

unread,
Mar 2, 2013, 11:33:21 AM3/2/13
to js-t...@googlegroups.com
> ES6 will introduce various ways of destructuring.

Check out my short post on destructuring with array and object
pattern: http://ariya.ofilabs.com/2013/02/es6-and-destructuring-assignment.html.




--
Ariya Hidayat, http://ariya.ofilabs.com
http://twitter.com/ariyahidayat
http://gplus.to/ariyahidayat
Reply all
Reply to author
Forward
0 new messages