Javascript jquery代理如何工作

Javascript jquery代理如何工作,javascript,jquery,Javascript,Jquery,我比什么都好奇。它如何将上下文传递给函数。它是否将函数包装在对象中?我确信有一些简单明了的代码可以在js中实现这一点,而不需要jquery代理 function abc(){ console.log(this.name); } var obj={name:"Something"}; $.proxy(abc,obj); 没有jquery代理,我怎么做?没有jquery,您可以使用: 如果你想与IE8兼容,你可以这样做 var newFunction = function(){ abc.cal

我比什么都好奇。它如何将上下文传递给函数。它是否将函数包装在对象中?我确信有一些简单明了的代码可以在js中实现这一点,而不需要jquery代理

function abc(){
  console.log(this.name);
}
var obj={name:"Something"};
$.proxy(abc,obj);

没有jquery代理,我怎么做?

没有jquery,您可以使用:

如果你想与IE8兼容,你可以这样做

var newFunction = function(){ abc.call(obj) };
以下是jQuery的工作方式:

// Bind a function to a context, optionally partially applying any
// arguments.
proxy: function( fn, context ) {
    var args, proxy, tmp;

    if ( typeof context === "string" ) {
        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 ) ) {
        return undefined;
    }

    // Simulated bind
    args = core_slice.call( arguments, 2 );
    proxy = function() {
        return fn.apply( context || this, args.concat( core_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 || jQuery.guid++;

    return proxy;
},

没有jQuery,您可以使用:

如果你想与IE8兼容,你可以这样做

var newFunction = function(){ abc.call(obj) };
以下是jQuery的工作方式:

// Bind a function to a context, optionally partially applying any
// arguments.
proxy: function( fn, context ) {
    var args, proxy, tmp;

    if ( typeof context === "string" ) {
        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 ) ) {
        return undefined;
    }

    // Simulated bind
    args = core_slice.call( arguments, 2 );
    proxy = function() {
        return fn.apply( context || this, args.concat( core_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 || jQuery.guid++;

    return proxy;
},

啊,当人们来投票而不留下评论的时候啊,当人们来投票而不留下评论的时候