Improving Function.prototype.bind

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Jan 5 08:46:33 PST 2012


all the time since I widely use private methods and I reuse them per each
created instance of a specific "class"

The boundTo() approach would make the method still private, immutable out
there, and reusable per each instance/object I need.

// stupid useless example ... just as concept
var Counter = (function () {
  // private
  function increase() {
    this.age++;
  }

  function Counter() {
    // none out there should be able to retrieve the bound function
    document.addEventListener("click", this.boundTo(increase), false);
  };
  Counter.prototype.age = 0;
  Counter.prototype.destroy = function () {
    // so that only this class scope can control things and nobody else
    document.removeEventListener("click", this.boundTo(increase), false);
  };
  return Counter;
}());

Last, but not least, I do duck typing and I often borrow methods around for
smaller common tasks

Best Regards,
    Andrea Giammarchi



On Thu, Jan 5, 2012 at 5:11 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

> that would not solve much 'cause you can bind a function to a single
> object and no more.
>
>
> Isn’t that the most frequent use case? Do you have examples where you bind
> the same function to several objects?
>
> --
> 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/20120105/f0cb262c/attachment-0001.html>


More information about the es-discuss mailing list