function() {}.bind(this) -> this.function(){} ?

Axel Rauschmayer axel at rauschma.de
Thu Dec 29 15:38:47 PST 2011


Yes, block lambdas are awesome and will nearly eliminate the "dynamic this versus lexical this" quirk from JavaScript.

    var obj = {
        foo: function () {
            console.log("hi!");
        },
        bar: function () {
            // block lambdas = lexical `this`
            // = picks up `this` from surrounding function
            window.addEventListener('load', {||
                this.foo(); // "hi!"
            }, false);
        }
    }


On Dec 29, 2011, at 20:13 , Rick Waldron wrote:

> IIRC, the block lambda proposal covers this (pun might be intended)
> 
> window.foo = function() {
>   console.log("hi!");
> };
> 
> window.addEventListener('load', {||
>   this.foo(); // "hi!"
> }, false);
> 
> 
> http://wiki.ecmascript.org/doku.php?id=strawman:block_lambda_revival#semantics

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111230/266ae2b5/attachment-0001.html>


More information about the es-discuss mailing list