未定义,typeof undefined,hasOwnProperty [英] Undefined, typeof undefined, hasOwnProperty
问题描述
拿这个片段,
var a = {
}
if(typeof a.c === 'undefined'){
console.log('inside if');
}
if(a.c === undefined){
console.log('inside if');
}
两个如果
结果在 true
中。 特定于某些浏览器的两种语句是否存在差异?
另外,在我的上一个项目中,我已经使用了 typeof ac =='undefined'
多次检查 json
数据中的值。
Also, in my last project I have already used typeof a.c == 'undefined'
numerous times to check values in json
data.
现在,我知道这不是好方法,因为某些值也可能是 undefined
,所以我的逻辑会失败。
Now, I know that this is not good way, as some value may be undefined
too, so my logic will fail.
我应该使用 hasOwnProperty
。
但是我确信没有值 undefined
,我可以使用 typeof ac =='undefined'
代替 hasOwnProperty
或者我应该用 hasOwnProperty
更改我所有的类型
p>
But I am sure that no value will be undefined
, can I use typeof a.c == 'undefined'
in place of hasOwnProperty
or should I change all my typeof
with hasOwnProperty
推荐答案
(更新:您可能想看看这个问题:变量=== undefined vs. typeof variable ===" undefined" )。
(UPDATE: You might want to check out this question: variable === undefined vs. typeof variable === "undefined").
在非常旧浏览器(Netscape 2,IIRC,可能还有IE 4或更低版本)中,您无法将值与<$ c $进行比较c> undefined ,因为那导致了错误。但是,在任何(半)现代浏览器中,没有理由检查 typeof value ==='undefined'
而不是 value === undefined
(有些人可能已经重新定义了变量 undefined
的偏执狂除外)。
In very old browsers (Netscape 2, IIRC, and possibly IE 4 or lower), you couldn’t compare a value with undefined
, since that caused an error. In any (semi-) modern browser, however, there’s no reason to check typeof value === 'undefined'
instead of value === undefined
(except for paranoia that somebody might have redefined the variable undefined
).
hasOwnProperty
用于不同的目的。它检查对象是否具有给定名称的属性而不是其原型;即无论继承属性如何。如果你想检查一个对象是否包含某个属性,是否继承,你应该使用 if('c'in a){
...
hasOwnProperty
serves a different purpose. It checks if the object has a property with the given name, and not its prototype; i.e. regardless of the inherited properties. If you want to check whether an object contains a certain property, inherited or not, you should use if ('c' in a) {
...
但基本上,这些可能都有效:
But basically, these will probably all work:
if (a.c === undefined) console.log('No c in a!');
if (typeof a.c === 'undefined') console.log('No c in a!');
if (!('c' in a)) console.log('No c in a!');
if (!a.hasOwnProperty('c')) console.log('No c in a!');
主要区别在于:
-
ac === undefined
如果有人做了undefined ='defined'将会产生意外结果
或一些这样的技巧; -
!(a中的'c')
不太可读(恕我直言) -
!a.hasOwnProperty('c')
如果对象<$,将返回false
c $ c> a 不包含属性c
,但其原型确实如此。
a.c === undefined
will produce an unexpected result if someone has doneundefined = 'defined'
or some such trick;!('c' in a)
is not very readable (IMHO)!a.hasOwnProperty('c')
will returnfalse
if the objecta
doesn’t contain a propertyc
, but its prototype does.
就个人而言,我更喜欢第一个,因为它更具可读性。如果你是偏执狂并希望避免重新定义 undefined
的风险,请将代码包装在一个自动执行的匿名函数中,如下所示:
Personally, I prefer the first since it’s more readable. If you’re paranoid and want to avoid the risk of a redefined undefined
, wrap your code in a self-executing anonymous function as follows:
(function (undefined) {
// in here, 'undefined' is guaranteed to be undefined. :-)
var a = {
};
})();
这篇关于未定义,typeof undefined,hasOwnProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!