绑定到`undefined`而不是`null`有什么好处 [英] what's the benefit of binding to `undefined` instead of `null`

查看:98
本文介绍了绑定到`undefined`而不是`null`有什么好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到,当一个函数需要在没有特定上下文的情况下使用绑定参数调用时, 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?

推荐答案


绑定到<的好处是什么? code> undefined 而不是 null

我认为没有。来自 10.4.3输入功能代码



  1. 如果功能代码是严格代码,设置 ThisBinding thisArg

  2. 否则如果 thisArg null undefined ,设置 ThisBinding 到全局对象。

  3. ...

  1. If the function code is strict code, set the ThisBinding to thisArg.
  2. Else if thisArg is null or undefined, set the ThisBinding to the global object.
  3. ...


所以,如果代码并不严格,在这两种情况下这个都将被设置为全局对象。

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`有什么好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆