-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Description
ECMAScript 中的 apply() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。
下面我们编写一个函数 myapply 来模拟 apply 方法,以此加深对 apply 的理解。
Function.prototype.myApply = function(context, arr) {
context = context || window;
context.fn = this;
var result;
if (!arr) {
result = context.fn();
} else {
var args = [];
for (var i = 0; i < arr.length; i++) {
args.push('arr[' + i + ']');
}
result = eval('context.fn(' + args + ')');
}
delete context.fn;
return result;
}Metadata
Metadata
Assignees
Labels
No labels