zoukankan      html  css  js  c++  java
  • 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;
        },
    
        ...............
    
    });
  • 相关阅读:
    The OpenGL pipeline
    HLS协议实现
    用C++设计一个不能被继承的类
    Ansible@一个高效的配置管理工具--Ansible configure management--翻译(八)
    史上最简单的软件破解——5行脚本代码完美破解99%的过期软件
    oracle11g创建新的用户和改动最大连接数
    【SICP感应】1 工艺和替代模式
    ant利用先进,ant订单具体解释,ant包,ant包装删除编译jar文件
    SqlServer表EXCEL数据复制的另一种方法
    【摘要干】如何执飞前写商业计划?
  • 原文地址:https://www.cnblogs.com/gongshunkai/p/5904288.html
Copyright © 2011-2022 走看看