arrow function syntax simplified

Russell Leggett russell.leggett at gmail.com
Tue Mar 27 21:14:41 PDT 2012


On Mar 27, 2012, at 10:36 PM, Kevin Smith <khs4473 at gmail.com> wrote:

> But aren't non-BTFs rare based on your (revised and original) measurements?
> 
> When you take out the object literal methods, that's right.
> 
> The precise way to say it would be that ~90% of function expressions in the code I analyzed were either object literal methods or did not depend on dynamic |this|.
> 
> That's exactly the reason I'm suggesting lexically bound |this| for "->" functions, because outside of "methods" (which already have shorter syntax, either via object literal extensions or classes), there doesn't appear to be a great need for function expressions with dynamic |this|.

I'm sure this is a bit of a tangent, but the other major related case is passing a "method" as an argument but needing to retain the correct "this". Obviously, that is what bind was meant for, but that is inconvenient when passing methods for the same reason it would be inconvenient for anonymous functions.

I guess what I'm proposing is a syntax supported version of bind that is independent of shorter function syntax entirely. Don't  hold me to the syntax, because I don't think its right, its just to give you an idea.

	//let say I have some object foo with a method bar that I want called as a callback
	//right now I have to use bind, or wrap the call in an additional function
	needsCallback(foo.bar.bind(foo);

	//also happens inside the class itself, and you'll see
	needsCallback(this.bar.bind(this));

	//and finally, the case we're addressing now
	needsCallback(function(x,y){
		if(x){
			this.doX(x);
		else{
			this.doY(y);
		}
	}.bind(this));
	

What if we had a shorthand for bind that worked in both of these cases

	//here the # acts as both a . and bind in one
	needsCallback(foo#bar)
	needsCallback(this#bar)

	//in this case, it obviously doesn't access a property, it just does a bind
	needsCallback(this#function(x,y){
		if(x){
			this.doX(x);
		else{
			this.doY(y);
		}
	});
	//or with the new syntax
	needsCallback(this#(x,y)->{
		...
	});

I'm guessing this idea is a stretch or possibly even suggested already and tossed out, but it came to me as a possibility and I thought I would mention it.

- Russ

> 
> kevin
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120328/523d3f8a/attachment.html>


More information about the es-discuss mailing list