Improving Function.prototype.bind

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Jan 9 00:18:43 PST 2012


I would be more than happy to go on with "libraries first" approach as long
as we don't have to wait 6 years before a needed feature is going to be
considered.

I have also raised the problem because it is in my opinion underestimated
but mainly because I would like to avoid the fragmentation of
implementations that Function#bind had indeed in its past.

My proposal was more like: would an Object.prototype.boundTo method make
sense?
If in this discussion list the outcome would be "Yes, it does" then me and
other libraries author may already consider, at least, the proposal, and
implement the best way they want with or without WeakMap shims in the
meanwhile.

If the answer is "No, bind should never be objects matter" then I could
come up with different proposal as long as we agree on a solution that
could be considered in TC39 too.

As summary, I am trying to find a "direction" able to solve this problem.
Even an interface, as common implementation independent direction, would be
fine ... something based on handleEvent property, another pattern rarely
seen out there, but with clear idea of how the whole problem would be
solved ... example:

// does not have to be in the global Object, just saying
Object.prototype.handleEvent = function handleEvent(evt) {
  // if there is some standard/agreed way to invoke meant method ...
  // let's use this pattern ...
  this["onEvent" + evt.type.slice(0, 1).toUpperCase() +
evt.type.slice(1)](evt);
};

// let's say that if the object is an object
// no need to implement handleEvent
// because everything is handled automagically
// behind the scene ...
window.addEventListener("resize", {
  onEventResize: function (evt) {
    window.removeEventListener(evt.type, this, false);
  }
}, false);

So that if handleEvent is not explicit, the onEvent + event.type will be
fired ... I am not sure it's clear what I mean ... but this would be
another proposal I'd like to hear feedbacks about

br
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120109/cecb7023/attachment.html>


More information about the es-discuss mailing list