问题描述
我经常看到,当一个函数需要在没有特定上下文的情况下使用绑定参数调用时, undefined
通常比 null 作为上下文的选择,如:
I often see that when a function needs to be called with bound parameters in no particular context the undefined
is more often than not is preferred over the null
as a choice of context, as in:
f.call(undefined, param1, param2)
优先于:
f.call(null, param1, param2)
我'我想知道是否有任何特殊原因?
I'm wondering if there is any particular reason for this?
推荐答案
我认为没有。来自:
- 如果功能代码是,设置到 thisArg 。
- 否则如果 thisArg null 或 undefined ,设置到全局对象。
- ...
- If the function code is strict code, set the ThisBinding to thisArg.
- Else if thisArg is null or undefined, set the ThisBinding to the global object.
- ...
所以,如果代码并不严格,在这两种情况下这个
都将被设置为全局对象。
So, if the code is not strict, in both cases this
will be set to the global object.
但如果代码是严格的,此
实际上将是 null
或 undefined
。可以实现 f
来区分这些情况,但这对我来说似乎不太可能。
But if the code is strict, this
will actually either be null
or undefined
. f
could be implemented to distinguish these cases, but that doesn't seem to be a very likely scenario to me.
这篇关于绑定到`undefined`而不是`null`有什么好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!