1

call实现代码

        Function.prototype.newCall = function(){
            var ctx = arguments[0] || window
            ctx.dx = this
            var args = []
            for(var i=1; i<arguments.length; i++){
                args.push('arguments['+ i +']')
            }
            var result = eval('ctx.dx('+args.join(',')+')')
            delete ctx.dx
            return result
        }

apply实现代码

            Function.prototype.newApply = function(ctx, arr){
                var ctx = ctx || window
                ctx.dx = this
                if(arr){
                    var args = []
                    for(var i=0; i<arguments.length; i++){
                        args.push('arguments['+ i +']')
                    }
                    var result = eval('ctx.dx('+args.join(',')+')')
                    delete ctx.dx
                    return result
                }else{
                    ctx.dx()
                    delete ctx.dx
                    return result                
                }        
            }

joga
23 声望0 粉丝