Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 实现自定义绑定问题_Javascript - Fatal编程技术网

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我正在尝试实现我自己的绑定函数。已添加预期结果。