绑定到`undefined`而不是`null`有什么好处

时间:2021-07-17 11:50:43

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:

我经常看到,当一个函数需要在没有特定上下文的情况下使用绑定参数调用时,undefined通常比null更适合作为上下文的选择,如:

f.call(undefined, param1, param2)

is preferred over:

优先于:

f.call(null, param1, param2)

I'm wondering if there is any particular reason for this?

我想知道这有什么特别的原因吗?

2 个解决方案

#1


6  

What's the benefit of binding to undefined instead of null?

绑定到undefined而不是null有什么好处?

I don't think there is any. From 10.4.3 Entering Function Code:

我认为没有。从10.4.3输入功能代码:

  1. If the function code is strict code, set the ThisBinding to thisArg.
  2. 如果函数代码是严格代码,请将ThisBinding设置为thisArg。

  3. Else if thisArg is null or undefined, set the ThisBinding to the global object.
  4. 否则,如果thisArg为null或未定义,则将ThisBinding设置为全局对象。

  5. ...

So, if the code is not strict, in both cases this will be set to the global object.

因此,如果代码不严格,则在两种情况下都将设置为全局对象。

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.

但是如果代码是严格的,那么实际上它将为null或未定义。可以实施f来区分这些情况,但这对我来说似乎不太可能。

#2


1  

In javascript, undefined extends past the scenario where a value has not been set. For instance, if you are looking for a property on a model that does not exist, it will yield undefined.

在javascript中,undefined延伸到尚未设置值的场景。例如,如果要在不存在的模型上查找属性,则会产生未定义的属性。

Its not that odd either to have code like this:

拥有这样的代码并不奇怪:

var k;

//do something (possibly setting k)

//做某事(可能设置k)

alert(k);

If the value has not been set, it will be undefined rather than null.

如果尚未设置该值,则它将是未定义的而不是null。

Long story short, it is still a preference, but by using undefined you are more likely to catch the cases where values have not been initialized or try accessing properties of objects that do no exist.

简而言之,它仍然是一个偏好,但是通过使用undefined,您更有可能捕获尚未初始化值的情况或尝试访问不存在的对象的属性。

Not sure this answers your question.

不确定这会回答你的问题。

#1


6  

What's the benefit of binding to undefined instead of null?

绑定到undefined而不是null有什么好处?

I don't think there is any. From 10.4.3 Entering Function Code:

我认为没有。从10.4.3输入功能代码:

  1. If the function code is strict code, set the ThisBinding to thisArg.
  2. 如果函数代码是严格代码,请将ThisBinding设置为thisArg。

  3. Else if thisArg is null or undefined, set the ThisBinding to the global object.
  4. 否则,如果thisArg为null或未定义,则将ThisBinding设置为全局对象。

  5. ...

So, if the code is not strict, in both cases this will be set to the global object.

因此,如果代码不严格,则在两种情况下都将设置为全局对象。

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.

但是如果代码是严格的,那么实际上它将为null或未定义。可以实施f来区分这些情况,但这对我来说似乎不太可能。

#2


1  

In javascript, undefined extends past the scenario where a value has not been set. For instance, if you are looking for a property on a model that does not exist, it will yield undefined.

在javascript中,undefined延伸到尚未设置值的场景。例如,如果要在不存在的模型上查找属性,则会产生未定义的属性。

Its not that odd either to have code like this:

拥有这样的代码并不奇怪:

var k;

//do something (possibly setting k)

//做某事(可能设置k)

alert(k);

If the value has not been set, it will be undefined rather than null.

如果尚未设置该值,则它将是未定义的而不是null。

Long story short, it is still a preference, but by using undefined you are more likely to catch the cases where values have not been initialized or try accessing properties of objects that do no exist.

简而言之,它仍然是一个偏好,但是通过使用undefined,您更有可能捕获尚未初始化值的情况或尝试访问不存在的对象的属性。

Not sure this answers your question.

不确定这会回答你的问题。