Function.prototype.bind is a feature of the newest JavaScript specification that, among other things, lets you write event handlers without inline closures. (The spec is on page 119 of the ECMAScript 5 standard for anyone who cares.) It's already available natively in Firefox 4 and coming soon to other browsers.

If we add a few lines to Drupal.js, we can make the same functionality available across browsers, which will let contrib write much cleaner JavaScript - and D8 core can clean up its act as well. Patch forthcoming.

CommentFileSizeAuthor
#3 1025626-3-bind.patch982 bytesksenzee
#1 1025626-1-bind.patch1.71 KBksenzee
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ksenzee’s picture

Status: Active » Needs review
FileSize
1.71 KB

Note that the implementation in this patch is based on http://webreflection.blogspot.com/2010/02/functionprototypebind.html, which has an MIT-style license. I've written the author for permission to include it in Drupal as GPL code.

Status: Needs review » Needs work

The last submitted patch, 1025626-1-bind.patch, failed testing.

ksenzee’s picture

Status: Needs work » Needs review
FileSize
982 bytes

I didn't hear from the author of the implementation from #1, so here's an updated patch. It's loosely based on https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Fun....

nod_’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs review » Needs work
Issue tags: +JavaScript clean-up

I'd like that too. There were talk of using underscore in core to help with functional kind of things.

Since it's for D8 now and that IE9+ supports it, we'd need this polyfill only for IE8 #1252178: Add Modernizr to core. Maybe see there if something is possible.

On a related note, I'd like to be able to use things like forEach classList where an IE8-only polyfill is needed, might be a good thing to extend the scope of this issue?

nod_’s picture

Status: Needs work » Closed (works as designed)
Issue tags: +ie8

Annd we don't need to care about it since D8 is IE9+ now :) tagging to IE8 module though.

jessebeach’s picture