call的特点
call模拟实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Function.prototype.call = function (context) { var context = context ? Object(context) : window; context.fn = this;
var args = []; for(var i = 1, len = arguments.length; i < len; i++) { args.push('arguments[' + i + ']'); }
var result = eval('context.fn(' + args +')'); delete context.fn; return result; }
|
apply 模拟实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Function.prototype.apply = function (context, args) { var context = context ? Object(context) : window; context.fn = this; var result = null; if (!args) { result = context.fn() } else { arr = []; for (var i = 0, len = args.length; i < len; i++) { arr.push('args[' + i + ']'); } result = eval('context.fn(' + arr + ')'); } delete context.fn; return result; }
|
-------------本文结束感谢您的阅读-------------