Bug Tracker

Opened 13 years ago

Closed 12 years ago

#5289 closed enhancement (fixed)

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:


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:  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 ) )};

Change History (2)

comment:1 Changed 12 years ago by Rick Waldron

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.

comment:2 Changed 12 years ago by addyosmani

Keywords: static support bind added
Resolution: fixed
Status: newclosed

As this has been addressed already I'm closing for now.

Note: See TracTickets for help on using tickets.