JavaScript undefined替换为null [英] JavaScript undefined replaced with null
问题描述
在JavaScript中 undefined
可以重新分配,因此通常建议创建一个自执行函数,以确保undefined实际上是未定义的。作为替代方案 null
和 undefined
肯定是 ==
但是是否任何其他值松散等效于 null / undefined
?
In JavaScript undefined
can be reassigned, so it is often advised to create a self executing function that assures undefined is actually undefined. As an alternative null
and undefined
are definitely ==
but are any other values loosely equivalent to null/undefined
?
基本上你可以安全地替换它:
Basically can you safely replace this:
(function(undefined){
window.f = function(obj){
if(obj===undefined || obj===null ){
alert('value is undefined or null');
}
}
})();
with:
window.f = function(obj){
if(obj==null){
alert('value is undefined or null');
}
}
如果以上是100%安全,为什么不' JavaScript社区/库完全删除 undefined
并使用较短的 x == null
条件来检查 null
/ undefined
一次?
If the above is 100% safe, why doesn't the JavaScript community/libraries drop undefined
altogether and use the shorter x == null
conditional to check for both null
/undefined
at once?
编辑:
我从未见过某人实际上代表未知价值,其中未定义vs null
?我从未见过这种情况,这也是我最初提出这个问题的原因。它似乎是两个令人难以置信的混淆值,从未在其原始意图中使用。标准化所有内容以进行比较 obj == null
对大小有益并避免重新分配的任何问题。一切都会继续工作
I have never seen someone actually represent an "unknown value" with 'undefined' vs null
? I have never seen this scenario, and is why I originally asked the question. It just appears to be two incredibly confused values that are never used in their original intent. Standardizing everything to do a comparison obj==null
would be beneficial for size and avoid any issues with reassignment. Everything would continue to work
var obj={};
obj.nonExistantProperty==null // true
var x;
ix==null // true
function(obj){
obj==null // true
}
此规则的一个例外似乎是在投射 undefined
/ null
为整数。这是一个相当年龄的情况,但绝对应该注意。
The one exception to this rule appears to be when casting undefined
/null
to an integer. This is a pretty age case scenario, but definitely should be noted.
+(null)== 0
而
isNaN(+ undefined)
考虑到NaN是JavaScript中唯一的值而不是等于它自己,你可以做一些非常疯狂的事情,比如:
Considering NaN is the only value in JavaScript not equal to itself, you can do some pretty crazy things like:
+undefined == +undefined // false
+null == +null // true
使用 null
作为松散的平等 ==
替换 undefined
是安全的,前提是你不打算将值转换为整数。这是一个非常优秀的情况。
Using null
as a loose equality ==
drop in replacement for undefined
is safe, provided you don't plan to cast the value to an integer. Which is a pretty edge case scenario.
推荐答案
抽象等式算法来自语言规范的第11.9.3节,定义为 ==
和!=
并定义它们
The abstract equality algorithm from section 11.9.3 of the language spec is what defined ==
and !=
and it defines them such that
null == void 0
null == null
void 0 == null
其中 void 0
只是说 undefined
的可靠方式(见下文),所以问题的答案是肯定的, null
等于未定义及其本身而已。
where void 0
is just a reliable way of saying undefined
(see below) so the answer to your question is yes, null
is equal to undefined and itself and nothing else.
规范的相关部分是
1. If Type(x) is the same as Type(y), then
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
...
2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.
...
如果你担心的话 undefined
意思是通常意味着什么,而是使用 void 0
。
If you're worried about undefined
meaning something other than what it normally means, use void 0
instead.
null == void 0 // True
({}).x === void 0 // True
"undefined" === typeof void 0 // True
(function () {})() === void 0 // True
(undefined = 42,
undefined === void 0) // False
"undefined" === typeof undefined // False
"undefined" === typeof void 0 // True
来自语言规范:
11.4.2无效运算符
生产UnaryExpression: 无效 UnaryExpression 评估如下:
The production UnaryExpression : void UnaryExpression is evaluated as follows:
- 让
EXPR
是评估 UnaryExpression /的结果。 - 致电
GetValue(expr)
。 - 返回未定义。
- Let
expr
be the result of evaluating UnaryExpression/. - Call
GetValue(expr)
. - Return undefined.
所以 void
前缀运算符会计算其参数并返回特殊值 undefined ,而不管全局变量是什么undefined
已更改(或者是否定义了 undefined
)。
So the void
prefix operator evaluates its argument and returns the special value undefined regardless of to what the global variable undefined
has been changed (or whether undefined
is defined :).
编辑:在回应评论时,
如果您正在处理区分两者的库代码,那么您需要处理差异。语言委员会标准化的一些新库确实忽略了差异: JSON.stringify([void 0])===[null]
但是有太多那里的代码巧妙地区别对待它们,还有其他差异:
If you are dealing with library code that distinguishes between the two, then you need to deal with the difference. Some of the new libraries standardized by the language committee do ignore the difference : JSON.stringify([void 0]) === "[null]"
but there is too much code out there that treats them subtly differently, and there are other differences :
+(null) === 0
isNaN(+undefined)
"" + null === "null"
"" + undefined === "undefined"
如果您正在编写任何类型的库来生成文本或序列化/反序列化,并且您想要将两者混淆,那么您无法传递 undefined
通过并期望它表现为 null
- 您需要明确地将输入标准化为其中一个。
If you're writing any kinds of libraries that produce text or serialize/deserialize and you want to conflate the two then you can't pass undefined
through and expect it to behave as null
-- you need to explicitly normalize your inputs to one or the other.
这篇关于JavaScript undefined替换为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!