codeing or artist ?
记得大学第一节编程课,教授说,"如果一件事儿有对错,那么是科学。如果有美丑好坏,那么是艺术。" 一个能顺利运行还能让人阅读时体验思维美妙的代码,就是艺术和科学的结合。能运行的程序并不是好程序,能当作文章来读的才是。在我看来代码是一种特殊的文体,程序猿其实会写诗。

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

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

});

 

posted on 2016-09-24 22:19  codeing-or-artist-??  阅读(186)  评论(0编辑  收藏  举报