call實現代碼javascript
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實現代碼java
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 } }