[英]Why does isNil method in Lodash use null instead of undefined?
Why does isNil
method in Lodash use null
instead of undefined
? 为什么
isNil
方法使用null
而不是undefined
?
function isNil(value) {
return value == null;
}
It makes no difference either way in terms of the logic using null
or undefined
as null == undefined == true
, but using null
instead of undefined
would make the file size smaller by 5 bytes. 对于使用
null
或undefined
为null == undefined == true
的逻辑而言,这两种方式都没有区别,但是使用null
代替undefined
会使文件大小减小5个字节。
It's simply done to save a few bytes making the file smaller and faster to download from the server. 只需保存一些字节即可,使文件更小, 更快地从服务器下载。
To understand this a bit better, it's important to note that lodash
is using ==
here instead of ===
. 为了更好地理解这一点,必须注意
lodash
在这里使用==
而不是===
。
Take the following example: 请看以下示例:
console.log(null == undefined); // true
console.log(null === undefined); // false
By using ==
(double equals), lodash is utilizing type coercion where null
and undefined
will be coerced to falsy values. 通过使用
==
(双等于),lodash使用了强制类型,其中null
和undefined
将被强制为伪造的值。 As a result, null == undefined
is true
. 结果,
null == undefined
为true
。
However, if using ===
(triple equals), no coercion is enforced, which means the types must be identical , and as we know null
is not identical to undefined
. 但是,如果使用
===
(三倍等于),则不会强制执行强制,这意味着类型必须相同 ,并且我们知道null
与undefined
不相同。 As a result, null === undefined
is false
. 结果,
null === undefined
为false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.