Javascript 实现自定义绑定问题
我正在尝试实现本机绑定功能。然而,当我尝试测试它时,我没有得到预期的输出 谁能告诉我哪里出了问题Javascript 实现自定义绑定问题,javascript,Javascript,我正在尝试实现本机绑定功能。然而,当我尝试测试它时,我没有得到预期的输出 谁能告诉我哪里出了问题 Function.prototype.myBind=Function(…args){ const self=这个; 返回函数(){ 返回self.apply(此参数为args); } }; 函数demo(){ 返回{ 名字:“詹姆斯·邦德”, 打印名:函数(args){ console.log(this.name,args); } }; } 常数测试={ 名字:“水族人” }; log(demo(
Function.prototype.myBind=Function(…args){
const self=这个;
返回函数(){
返回self.apply(此参数为args);
}
};
函数demo(){
返回{
名字:“詹姆斯·邦德”,
打印名:函数(args){
console.log(this.name,args);
}
};
}
常数测试={
名字:“水族人”
};
log(demo().printName.myBind(test)('2020');
//期望
log(demo().printName.bind(test)('2020'))代码>您需要像这样绑定到参数:
Function.prototype.myBind = function(binder) {
const self = this;
return function() {
return self.apply(binder, arguments);
}
};
意外的结果是什么?预期产量是多少?注意,this.name
引用了窗口的name
属性。为什么内置的函数.prototype.bind()
没有被使用?@guest271314我正在尝试实现我自己的绑定函数。已添加预期结果。