Side navigation
#5289 closed enhancement (fixed)
Opened September 24, 2009 12:02PM UTC
Closed October 21, 2010 07:32PM UTC
Provide static support for Function.prototype.bind
Reported by: | andre1 | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | 1.4 |
Component: | core | Version: | 1.3.2 |
Keywords: | static support bind | Cc: | |
Blocked by: | Blocking: |
Description
binding context and arguments to functions is very useful, especially when dealing with events.
Luckily JavaScript 3.1 and it's Function.prototype.bind is coming. But since we won't see that in all browsers anytime soon, we could provide a static variant that uses the 3.1 variant if present.
tc39-2009-025 p.114:
15.3.4.5 Function.prototype.bind (thisArg [, arg1 [, arg2, …]]) The bind method takes one or more arguments, thisArg and (optionally) arg1, arg2, etc, and returns a new function object by performing the following steps:
Example code (name should probably be something different to not confuse with jQuery.bind):
function bind() { // Binds arguments to a function, so when you call the returned wrapper function, // arguments are intact and arguments passed to the wrapper function is appended. // first argument is function, second is 'this' and the rest is arguments var args = jQuery.makeArray(arguments), __fn = args.shift(); if ( __fn.bind !== undefined ) { return __fn.bind( args ); } return function(){return __fn.apply( args.shift(), args.concat( jQuery.makeArray(arguments) ) )}; }
Should additionally consider (ignore the name, like above):
{{{
function bindEvent()
{
// Same as bind(), but includes the arguments to the wrapper function first(ie event arguments) (prepended)
var args = jQuery.makeArray(arguments), __fn = args.shift();
return function(){return __fn.apply( args.shift(), jQuery.makeArray(arguments).concat( args ) )};
},
This has been addressed with the $.proxy() implementation. Additionally - requests to extend native objects will be denied in favor of extending jQuery with the functionality if the need is great.