未定义,typeof undefined,hasOwnProperty [英] Undefined, typeof undefined, hasOwnProperty

查看:125
本文介绍了未定义,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 done undefined = 'defined' or some such trick;
  • !('c' in a) is not very readable (IMHO)
  • !a.hasOwnProperty('c') will return false if the object a doesn’t contain a property c, 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屋!

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