-
-
Notifications
You must be signed in to change notification settings - Fork 109
Open
Description
Function.prototype.myBind = function(context){
if (context === null || context === undefined) {
// this参数如果是null/undefined,就指向window,node环境为global
try {
context = window;
} catch(e){
context = global;
}
} else {
context = Object(context);
}
const symbolKey = Symbol('特殊键,用来存储原始this,即原始function');
context[symbolKey] = this;
return function(...params){
context[symbolKey](...params);
Reflect.deleteProperty(context, symbolKey);
};
}
这样可以摆脱对call的依赖,而且保持和myCall、myApply实现原理的一致性
Metadata
Metadata
Assignees
Labels
No labels