jquery工具方法proxy

proxy : 改变this指向

使用方法1:
function show(){

  alert(this);

}
$.proxy(show,document)();  //document

使用方法2:
function show(n1,n2){

  alert(n1);
  alert(n2);

  alert(this);

}
$.proxy(show,document)(3,4);  //document  3  4
$.proxy(show,document,3,4)();  //document  3  4
$.proxy(show,document,3)(4);  //document  3  4

使用方法3:
var obj = {

  show:function(){

    alert(this);

  }

};

$(document).click($.proxy(obj,‘show‘)); //object

从proxy方法的源码可以看出,以下代码片段是针对 ‘ 使用方法3 ‘ 做了处理,它允许第二个参数为字符串,其实等同于:$(document).click($.proxy(obj.show,obj));

if ( typeof context === "string" ) {
    tmp = fn[ context ];
    context = fn;
    fn = tmp;
}

以下代码片段是针对传参的处理,先使用core_slice.call截取第3个参数到最后,再获取调用时的参数,最后合并。

// Simulated bind
args = core_slice.call( arguments, 2 );
proxy = function() {
    return fn.apply( context, args.concat( core_slice.call( arguments ) ) );
};

proxy完整的代码:

var core_slice = Array.prototype.slice,

    ....................

jQuery.extend({

    ......................

    // A global GUID counter for objects
    guid: 1,

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

        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, 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;
    },

    ...............

});
时间: 2024-07-31 04:09:22

jquery工具方法proxy的相关文章

Java程序员的JavaScript学习笔记(9—— jQuery工具方法)

计划按如下顺序完成这篇笔记: 1.    理念. 2.    属性复制和继承. 3.    this/call/apply. 4.    闭包/getter/setter. 5.    prototype. 6.    面向对象模拟. 7.    jQuery基本机制. 8.    jQuery选择器. 9.    jQuery工具方法. 10.    jQuery-在"类"层面扩展. 11.    jQuery-在"对象"层面扩展. 12.    jQuery-扩

Java程序猿的JavaScript学习笔记(9—— jQuery工具方法)

计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript学习笔记(3--this/call/apply) Java程序猿的JavaScript学习笔记(4--this/闭包/getter/setter) Java程序猿的JavaScript学习笔记(5--prototype) Java程序猿的JavaScript学习笔记(6--面向对象模拟) Java程

jquery源码解析:jQuery工具方法详解4

jQuery的工具方法,其实就是静态方法,源码里面就是通过extend方法,把这些工具方法添加给jQuery构造函数的. jQuery.extend({ ...... guid: 1, //唯一标识符,跟事件有关.举个例子:function show(){alert(this);}, $("#input1").click(show),$("#input2").click(function(){$("#input1").off()}),这里的sho

jquery源码解析:jQuery工具方法详解1

jQuery的工具方法,其实就是静态方法,源码里面就是通过extend方法,把这些工具方法添加给jQuery构造函数的. jQuery.extend({       //当只有一个对象时,就把这个对象中的属性和方法扩展到this对象中,这里的this指向jQuery expando: "jQuery" + ( core_version + Math.random() ).replace( /\D/g, "" ), //唯一性,core_version 为jQuery

jquery源码解析:jQuery工具方法详解2

jQuery的工具方法,其实就是静态方法,源码里面就是通过extend方法,把这些工具方法添加给jQuery构造函数的. jQuery.extend({ ...... type: function( obj ) {    //$.type(),判断类型 if ( obj == null ) {   //null,undefined return String( obj );    //返回null,undefined字符串 }     //core_toString = {}.toString 

jquery源码解析:jQuery工具方法详解3

jQuery的工具方法,其实就是静态方法,源码里面就是通过extend方法,把这些工具方法添加给jQuery构造函数的. jQuery.extend({ ...... each: function( obj, callback, args ) {   //$.each(arr , function(i,value){}),第三个参数用于内部调用.此方法就是来遍历数组的,然后取数组中的值进行显示.不能改变原数组arr,跟map一样,但是map返回新数组,而each返回原数组.这里跟原生的forEa

jquery工具方法

if($("#id").length>0){}else{}  判断对象是否存在 typeof($("select[name^='shijiActorSel_']").val()) != "undefined" 通过ID模糊查询,并查询子信息 var tabDiv =$("div[id^='tab_']"); if( 0 < tabDiv.length){     var table = tabDiv.find(&q

jquery源码解析:jQuery工具方法when详解

我们先来看when方法是如何使用的: var cb = $.when();   //when方法也是返回一个延迟对象,源码是return deferred.promise();返回的延迟对象不能修改状态 $.Deferred()也是返回一个延迟对象,那么它们的区别是什么呢?$.Deferred()只能针对一个延迟对象做成功,失败,进行中的操作,而$.when()可以针对多个延迟对象做以上操作.举个例子: function a(){ var cb = $.Deferred(); cb.resolv

jquery源码解析:jQuery工具方法Callbacks详解

我们首先来讲下Callbacks是如何使用的:第一个例子 function a(){} function b(){} var cb = $.Callbacks(); cb.add(a); cb.add(b); cb.fire();      //就会先执行a方法,再执行b方法 上面大概的意思是:add方法,就是把方法添加到数组list中,每执行一次,就添加一个方法.而fire方法,就是把list数组中的方法按顺序执行. 使用场景:第二个例子 function a(n){ } (function(