apply的具体实现
代码语言:javascript
复制Function.prototype.apply = function(context, arr) {
var context = Object(context) || window
context.fn = this
var result
if (!arr) {
result = context.fn()
} else {
var args = [];
for (var i = 0, len = arr.length; i < len; i ) {
args.push('arr[' i ']');
}
result = eval('context.fn(' args ')')
}
delete context.fn
return result
}
复制代码
call的具体实现
代码语言:javascript
复制Function.prototype.call = function(context) {
var context = 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
}
复制代码