[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: terse lambdas in ECMAScript 6
- From: Jay Carlson <nop@...>
- Date: Sat, 31 Mar 2012 01:14:48 +0000
Doug Crockford writes about a possible shorter function syntax for
ECMAScript 6, which also fixes up what "this" points at. (How "this"
works in JS is one of those things I work hard to forget.) You can
read about it at
http://www.yuiblog.com/blog/2012/03/30/what-is-the-meaning-of-this/
but the quick version:
(x) => x * x
is sugar for
function (x) { return x*x; }
and [[ (x) => {statements;} ]] evaluates to what you'd think.
Procedures constructed with the "fat arrow" syntax have "this" bound
to the "this" of the outer function (instead of the global object).
For function objects intended as methods you instead write
(this, x) => { this.property = x; }
which does the dynamic binding thing.
E4X seemingly didn't take over the world, so I don't know what the
practical effects of this language revision will be. I wonder if the
negatives of terse lambdas (as recently discussed) came up, although
fresh syntax was going to be needed for the binding issue anyway.
Jay
(who reads "var that=this;" and immediately gets Fatboy Slim's "Weapon
Of Choice" stuck in his head: "...you can blow with *this*, or you can
blow with *that*...or you can blow with us!" Is the moon halfway
between the gutter and the stars?)