-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Labels
Description
apply / call
区别
func.call(this, arg1, arg2);
func.apply(this, [arg1, arg2]);参数数量固定时用call,不确定时用apply。希望改变上下文环境后立即执行,用apply / call。
实现 apply
Function.prototype.myApply = function(context = window, arguments) {
if (!context) {
context = Object.create(null);
}
context.fn = this;
if (arguments) {
context.fn(...arguments);
} else {
context.fn();
}
delete context.fn;
};实现 call
Function.prototype.myApply = function(context = window, ...arguments) {
if (!context) {
context = Object.create(null);
}
context.fn = this;
if (arguments) {
context.fn(...arguments);
} else {
context.fn();
}
delete context.fn;
};