Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 js中的bind编程_Javascript - Fatal编程技术网

Javascript js中的bind编程

Javascript js中的bind编程,javascript,Javascript,我决定重新创建为绑定,以便进行练习。不幸的是,我遇到了一些问题,无法理解未定义输出的原因。 这是我的密码: var-alex={ 名字:“亚历克斯”, 姓:“姓”, } 让name=function(){ log(`this.firstname}${this.names}`); } Function.prototype.binding=函数(smth,…参数){ smth.\uuuuu proto\uuuuuuu.bindedfunct=this; 返回smth.bindedfunct; }

我决定重新创建为
绑定
,以便进行练习。不幸的是,我遇到了一些问题,无法理解
未定义
输出的原因。 这是我的密码:

var-alex={
名字:“亚历克斯”,
姓:“姓”,
}
让name=function(){
log(`this.firstname}${this.names}`);
}
Function.prototype.binding=函数(smth,…参数){
smth.\uuuuu proto\uuuuuuu.bindedfunct=this;
返回smth.bindedfunct;
}

说出你自己的名字您的函数
绑定
没有达到预期效果。你只是返回你传递的函数

为了实现与
bind
类似的功能,您需要使用
Function.prototype.call/apply
使用thisBinding调用函数

下面是一个使用
的示例实现

var-alex={
姓名:“亚历克斯”,
姓:“姓”,
}
让name=function(){
log(`${this.name}${this.姓氏}`);
}
Function.prototype.binding=函数(thisBinding,…args){
返回(…_参数)=>{
this.apply(thisBinding,[…args,[u args]);
}
}

命名你自己。binding(alex)(
你的函数
binding
没有达到你期望的效果。你只是返回你传递的函数

为了实现与
bind
类似的功能,您需要使用
Function.prototype.call/apply
使用thisBinding调用函数

下面是一个使用
的示例实现

var-alex={
姓名:“亚历克斯”,
姓:“姓”,
}
让name=function(){
log(`${this.name}${this.姓氏}`);
}
Function.prototype.binding=函数(thisBinding,…args){
返回(…_参数)=>{
this.apply(thisBinding,[…args,[u args]);
}
}

name yourself.binding(alex)(
示例代码下面给出了更详细的解释

const alex={
名字:“亚历克斯”,
姓:“姓”,
}
函数名(…args){
log(`this.firstname}${this.names}`);
log('args:',args);
}
Function.prototype.binding=函数(目标,…边界参数){
//要绑定的函数。
const boundFunction=this;
//绑定包装器函数。
返回函数(…args){
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
应用(目标,[…边界参数,…参数])
}
}
name yourself.binding(alex,'Hello')(alex.firstname,alex.姓氏)

.as控制台包装{max height:100%!important;top:0;}
示例代码下面给出了更详细的解释

const alex={
名字:“亚历克斯”,
姓:“姓”,
}
函数名(…args){
log(`this.firstname}${this.names}`);
log('args:',args);
}
Function.prototype.binding=函数(目标,…边界参数){
//要绑定的函数。
const boundFunction=this;
//绑定包装器函数。
返回函数(…args){
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
应用(目标,[…边界参数,…参数])
}
}
name yourself.binding(alex,'Hello')(alex.firstname,alex.姓氏)

。作为控制台包装{max height:100%!important;top:0;}
绑定应该做什么?是的,它…不是。你只需要调用
(nameofyourself)(
,它将
重置为
未定义
,并且
alex
不会被传递给任何对象。正确的实现不需要对
smth进行处理
smth
alex
。将
nameyouyself
分配给
alex.\uuuu proto\uuuu
上的属性。然后返回分配的内容。出于所有目的,
binding
只返回此
,其副作用是向
alex
添加属性。这不会“绑定”任何东西。
binding
应该做什么?是的,它…不是。你只需要调用
(命名你自己)(
,它将
重置为
未定义
,并且
alex
不会被传递给任何对象。正确的实现不需要对
smth进行处理
smth
alex
。将
nameyouyself
分配给
alex.\uuuu proto\uuuu
上的属性。然后返回分配的内容。出于所有目的,
binding
只返回此
,其副作用是向
alex
添加属性。这不会“束缚”任何东西。