[英]bind this is optional in class if no constructor?
I have below class, but I'm confused by the bind this here. 我在下课上,但是我对这里的绑定感到困惑。 The class don't have a constructor and super(), why did he need to do bind this on setAll method?
该类没有构造函数和super(),为什么他需要在setAll方法上进行绑定? I removed the bind this, the application still work and no error.
我删除了此绑定,该应用程序仍然可以正常工作并且没有错误。
class PushScheduleActions {
/**
* Request all API
*/
fetchAll(params) {
return dispatch => {
dispatch(params);
PushScheduleApi.getAll(params)
.then(this.setAll.bind(this));//why use bind this here?
.catch(this.setError.bind(this));
}
}
setAll(data) {
return data;
}
}
There is no relation between constructor, super and bind. 构造函数,super和bind之间没有关系。
What bind does? 什么绑定呢? it returns a new function with the context user passed.
它通过上下文用户返回一个新函数。
In your case, PushScheduleApi success case you are passing a new setAll function which is returned by bind method. 对于您的情况,PushScheduleApi成功的情况是您传递了一个新的setAll函数,该函数由bind方法返回。
since your setAll function doesn't use context, you don't need to pass the context. 由于您的setAll函数不使用上下文,因此不需要传递上下文。
dispatch is an arrow function, so context is inherited. dispatch是一个箭头函数,因此上下文是继承的。 So you dont need bind method.
因此,您不需要绑定方法。 you can simply use this.setAll
您可以简单地使用this.setAll
The bind() function creates a new bound function (BF). bind()函数创建一个新的绑定函数(BF)。 A BF is an exotic function object (a term from ECMAScript 2015 ) that wraps the original function object.
BF是包装原始函数对象的奇异函数对象( ECMAScript 2015中的术语)。 Calling a BF generally, results in the execution of its wrapped function.
通常调用BF会导致执行其包装函数。
For more detail follow this link i hope it will help you a lot to understand to bind() method Use of the JavaScript 'bind' method 有关更多详细信息,请单击此链接,希望它对您理解bind()方法有很大帮助, 使用JavaScript'bind'方法
The class don't have a constructor and super()
该类没有构造函数和super()
That's completely irrelevant. 那完全无关紧要。
why did he need to do bind this on setAll method?
他为什么需要在setAll方法上进行绑定?
The method only needs to be bound if 该方法仅在以下情况下需要绑定
this
and this
this
should refer to what this
refers to inside fetchAll
(likely an instance of PushScheduleActions
). this
应该是指什么this
是指内部fetchAll
(可能的实例PushScheduleActions
)。 Since setAll
doesn't reference this
, there is no reason to bind. 由于
setAll
没有引用this
,因此没有理由绑定。
However , the whole setAll
method is unnecessary. 但是 ,整个
setAll
方法是不必要的。 The code would work exactly the same without .then(this.setAll.bind(this))
. 如果没有
.then(this.setAll.bind(this))
代码将完全相同。 That makes me think that PushScheduleActions
is supposed to be subclassed and child classes are supposed to overwrite setAll
. 这使我认为
PushScheduleActions
应该被子类化 ,子类应该覆盖setAll
。 Now, the child class' setAll
might use this
and expect it to refer to the instance. 现在,子类的
setAll
可能会使用this
并期望它引用实例。 Since the parent class ( PushScheduleActions
) can't know that, binding the method is safer since it will ensure that child class implementations of setAll
will work regardless if they use this
or not. 由于父类(
PushScheduleActions
)不知道这一点,因此绑定该方法PushScheduleActions
安全,因为它将确保setAll
子类实现可以工作,无论是否使用this
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.