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

Rick Waldron waldron.rick at gmail.com
Thu Dec 29 11:13:17 PST 2011


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


Rick


On Thu, Dec 29, 2011 at 1:54 PM, John J Barton
<johnjbarton at johnjbarton.com>wrote:

> Sorry if this has been suggested before....
>
> I've stopped using the 'self=this' trick:
>    ...
>    var self = this;
>    window.addEventListener('load', function(event) {
>      self.foo();
>    }, false);
>
> in favor of bind():
>    ...
>    window.addEventListener('load', function(event) {
>      this.foo();
>    }.bind(this), false);
>
> It's fine, but I wonder if such code would be clearer if we could write:
>    ...
>    window.addEventListener('load', this.function(event) {
>      this.foo();
>    }, false);
>
> To me at least this more closely expresses my intent: I want an anonymous
> method of 'this' object, not an anonymous function.
>
> jjb
>
>
>
> _______________________________________________
> 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/20111229/fdba078c/attachment.html>


More information about the es-discuss mailing list