Basic Lambdas and Explicit Tail Calls Repackaged

Brendan Eich brendan at mozilla.com
Mon Dec 8 09:32:21 PST 2008


On Dec 8, 2008, at 12:38 AM, Michael Day wrote:

> Hi Brendan,
>
>> I would be surprised if we ever evolved a version that lost the  
>> default this binding whereby
>> var o = {m: function(){print(this.x);}, x: 42};
>> o.m();
>
> Right, but if it was a lambda instead:
>
> var o = {m: lambda(){print(this.x);}, x: 42};
> o.m();
>
> the options are:
>
> - Capture the 'this' value in scope when the lambda is defined, and  
> use that when the lambda is called.

This would break the pattern:

C.prototype = {
     m1: function(){return this.x1},
     m2: function(){return this.x2},
     ...
};

where (new C) is evaluated repeatedly and each instance delegates to  
C.prototype but of course |this| in the proto-methods binds to the  
directly-referenced instance.


> - Always return 'undefined' when 'this' is evaluated within a lambda.

Would frustrate any use of lambda (fun!) patterned on today's  
prototype method templates.

Why shadow an outer |this|?

It would be better to make |this| in a lambda body a static error.


> - Any others?
>
> Do either of these policies make lambdas useful language constructs  
> as compared with normal functions?


Compared with, or contrasted to?

|this| as a calling-expression-bound leading parameter is an odd duck.  
But it's part of the way object oriented JS is written.

Lambda as better function could lose it along with the arguments  
object, and people who prefer it would use lexical 'self' variable  
binding to make higher-integrity methods (no this jihacking; Mark's  
"Look Ma, no this!" post).

If lambdas are too different (TCP to the max) they are probably  
hazardous -- attractive nuisances for too many users of the language.

If lambdas are too much like functions, what's the point? We can work  
on briefer function syntax.

I think lambdas should not bind |this| in order to avoid dynamic | 
this| binding hazards without guessing at a single (and likely wrong,  
in light of standard JS OOP practices) object to bind unconditionally.

This does not mean an outer |this| would be visible in a lambda's  
body, however -- but it could. The choices seem to be:

1. Let outer |this| be visible in the lambda's body -- "lexical this".

2. Make |this| in a lambda body a static error.

/be


More information about the Es-discuss mailing list