]> source.dussan.org Git - jquery.git/commitdiff
Remove unnecessary usage of Function.prototype.bind (#7783) but maintain API. Also...
authorJohn Resig <jeresig@gmail.com>
Sun, 17 Apr 2011 21:11:40 +0000 (14:11 -0700)
committerJohn Resig <jeresig@gmail.com>
Sun, 17 Apr 2011 21:11:40 +0000 (14:11 -0700)
src/core.js
src/support.js
test/unit/core.js

index 89bbde5c010b1ce8e08b6b38b6cd16eb4d850c0e..a82a2fdced2018b9d0923f4c91854e1bcfd46dbd 100644 (file)
@@ -752,45 +752,23 @@ jQuery.extend({
        // Bind a function to a context, optionally partially applying any
        // arguments.
        proxy: function( fn, context ) {
-               var args, proxy;
-
-               // XXX BACKCOMPAT: Support old string method.
                if ( typeof context === "string" ) {
-                       fn = fn[ context ];
-                       context = arguments[0];
+                       var tmp = fn[ context ];
+                       context = fn;
+                       fn = tmp;
                }
 
                // Quick check to determine if target is callable, in the spec
                // this throws a TypeError, but we will just return undefined.
-               if ( ! jQuery.isFunction( fn ) ) {
+               if ( !jQuery.isFunction( fn ) ) {
                        return undefined;
                }
 
-               if ( jQuery.support.nativeBind ) {
-                       // Native bind
-                       args = slice.call( arguments, 1 );
-                       if ( args.length ) {
-                               proxy = Function.prototype.bind.apply( fn, args );
-                       } else {
-                               proxy = fn.bind( context );
-                       }
-               } else {
-                       // Simulated bind
-                       args = slice.call( arguments, 2 );
-                       if ( args.length ) {
-                               proxy = function() {
-                                       return arguments.length ?
-                                               fn.apply( context, args.concat( slice.call( arguments ) ) ) :
-                                               fn.apply( context, args );
-                               };
-                       } else {
-                               proxy = function() {
-                                       return arguments.length ?
-                                               fn.apply( context, arguments ) :
-                                               fn.call( context );
-                               };
-                       }
-               }
+               // Simulated bind
+               var args = slice.call( arguments, 2 ),
+                       proxy = function() {
+                               return fn.apply( context, args.concat( slice.call( arguments ) ) );
+                       };
 
                // Set the guid of unique handler to the same of original handler, so it can be removed
                proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
index 1bd35cab2d790656584994ea87b63205f5f15b1a..867e183675ece01c4458c490d3617c90079421c6 100644 (file)
@@ -77,10 +77,6 @@ jQuery.support = (function() {
                // Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribute (ie6/7)
                getSetAttribute: div.className !== "t",
 
-               // Test for presence of native Function#bind.
-               // Not in: >= Chrome 6, >= FireFox 3, Safari 5?, IE 9?, Opera 11?
-               nativeBind: jQuery.isFunction( Function.prototype.bind ),
-
                // Will be defined later
                submitBubbles: true,
                changeBubbles: true,
index cc744e7b1b4461f092ccb8322ba7f3ce405ca087..f0bf24cd5ad36ba2d5469ff8f8a1609f8b424ad1 100644 (file)
@@ -910,7 +910,7 @@ test("jQuery.isEmptyObject", function(){
 });
 
 test("jQuery.proxy", function(){
-       expect(6);
+       expect(7);
 
        var test = function(){ equals( this, thisObject, "Make sure that scope is set properly." ); };
        var thisObject = { foo: "bar", method: test };
@@ -921,6 +921,9 @@ test("jQuery.proxy", function(){
        // Basic scoping
        jQuery.proxy( test, thisObject )();
 
+       // Another take on it
+       jQuery.proxy( thisObject, "method" )();
+
        // Make sure it doesn't freak out
        equals( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );