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

var proxy = function(context,fn) {
    var args, proxy;

    if ( typeof fn !== 'function') {
        return undefined;
    }

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

    return proxy;
};


//调用1:
var show = function(){
    alert(this);
}
proxy(document,show)();  //document

//调用2:
var show = function(n1,n2){
    alert(n1*n2);
    alert(this);
}
proxy(document,show,3,4)();   //12   document
proxy(document,show)(3,4);   //12   document
proxy(document,show,3)(4);   //12   document

//调用3:
var obj = {
    show:function(n1,n2){
        alert(n1*n2)
        alert('obj -> show');
    }
};
document.onclick = proxy(obj,function(){
    this.show(3,4);
});

 

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