2009-04-22 109 views
65

注意:根据ECMAScript5.1, section 15.1.1.3,window.undefined是只读的。JavaScript:undefined!undefined?

  • 现代浏览器正确实现了这一点。例如:5.1的Safari,Firefox的7,铬20等
  • 未定义仍然多变在:铬14,...

当我最近集成Facebook ConnectTersus,我最初接收的尝试调用Facebook API函数时出现错误消息Invalid Enumeration ValueHandler already exists

原来,这个问题的原因是

object.x === undefined 

返回false时,有在“对象”没有财产“X”。

我工作围绕这一问题通过定期平等更换全等在两个类似Facebook的功能:

FB.Sys.isUndefined = function(o) { return o == undefined;}; 
FB.Sys.containsKey = function(d, key) { return d[key] != undefined;}; 

这使事情对我的工作,但似乎在Facebook的JavaScript代码和之间的某种碰撞的暗示我拥有。

这是什么原因造成的?

提示:有据可查的是undefined == nullundefined !== null。这不是问题。问题是我们如何得到undefined !== undefined

+5

有趣的是,我只是在我的控制台试过。 `var a = {}; a.b === undefined // true`。你确定你的`object.x === undefined`返回false是因为对象中没有字段x? – 2011-09-26 21:00:18

+1

“按照ECMAScript5.1,第15.1.1.3节,window.undefined是只读的。“ - Horray,因为在以前的版本中,有人可以在全球范围覆盖`undefined`,并且所有内容都会中断:( – Dan 2013-06-04 09:01:14

回答

59

事实证明,你可以设置window.undefined到任何你想要的,所以得到object.x !== undefined时object.x是真正不确定。在我的情况下,我无意中将undefined设置为null。

看到这样的情况,最简单的方法是:

window.undefined = null; 
alert(window.xyzw === undefined); // shows false 

当然,这是不可能发生的。在我的情况下,这个bug比较微妙,并且等同于以下情况。

var n = window.someName; // someName expected to be set but is actually undefined 
window[n]=null; // I thought I was clearing the old value but was actually changing window.undefined to null 
alert(window.xyzw === undefined); // shows false 
88

问题是未定义的比较null使用==给出了真实。 因此,对于不确定的常见检查是这样的:

typeof x == "undefined" 

这确保了变量的类型真的是不确定的。 A)

+60

)在这里的许多答案中,最大的误解是'undefined'是一个Javascript关键字,根本不是关键字。一个变量(大部分时间)恰好是未定义的,所以唯一一次“somevar === undefined”工作的时候,'undefined'变量还没有被定义,我见过代码(jcanvas),其中的类包装函数包含名为'undefined'的最后一个参数(从未使用过),以确保在函数范围内会有一个未定义的变量'undefined'。除了这样的特殊情况,使用'typeof'是唯一的“right”way。 – 2011-05-28 06:03:53

+1

这是否意味着,在未定义的位置,我也可以使用garglblarg,只要它永远不会被定义? – 2011-06-10 13:02:14

+1

@Dercsár:是的,`var foo; foo === undefined // true`。 – pimvdb 2011-08-15 09:44:39

2

A)。我永远都不会相信任何声称在没有用户编码的情况下生成代码的工具,而在用户使用图形工具的情况下,这种工具会翻倍。

B)。 Facebook Connect从未遇到任何问题。这是所有在浏览器中运行的普通旧JavaScript代码,无论你身在何处都可以使用undefined===undefined

总之,您需要提供证据表明您的object.x真的是未定义的,不为空或其他,因为我相信这是不可能的,我会把钱存在Tersus代码中存在的问题上。

15

这是使用==等号运算符而不是===的糟糕做法。

undefined === undefined // true 
null == undefined // true 
null === undefined // false 

object.x === undefined应该返回true如果x不明财产。

在章Bad Parts of JavaScript: The Good Parts,克罗克福德写入以下内容:

If you attempt to extract a value from an object, and if the object does not have a member with that name, it returns the undefined value instead.

In addition to undefined, JavaScript has a similar value called null. They are so similar that == thinks they are equal. That confuses some programmers into thinking that they are interchangeable, leading to code like

value = myObject[name]; 
if (value == null) { 
    alert(name + ' not found.'); 
} 

It is comparing the wrong value with the wrong operator. This code works because it contains two errors that cancel each other out. That is a crazy way to program. It is better written like this:

value = myObject[name]; 
if (value === undefined) { 
    alert(name + ' not found.'); 
} 
18

我想发布一些关于undefined的重要信息,初学者可能不知道。

请看下面的代码:

/* 
    * Consider there is no code above. 
    * The browser runs these lines only. 
    */ 

    // var a; 
    // --- commented out to point that we've forgotten to declare `a` variable 

    if (a === undefined) { 
     alert('Not defined'); 
    } else { 
     alert('Defined: ' + a); 
    } 

    alert('Doing important job below'); 

如果你运行该代码,其中变量a从未使用var, 你会得到一个错误异常和令人惊讶的看到没有警报都被宣布。

而不是'做重要工作以下',您的脚本将终止意外,在第一行抛出未处理的异常。


这里是唯一的防弹的检查方法undefined使用typeof关键字,其目的只是为了这样的目的:

/* 
    * Correct and safe way of checking for `undefined`: 
    */ 

    if (typeof a === 'undefined') { 
     alert(
      'The variable is not declared in this scope, \n' + 
      'or you are pointing to unexisting property, \n' + 
      'or no value has been set yet to the variable, \n' + 
      'or the value set was `undefined`. \n' + 
      '(two last cases are equivalent, don\'t worry if it blows out your mind.' 
      ); 
    } 

    /* 
    * Use `typeof` for checking things like that 
    */ 

这种方法适用于所有可能的情况。

的最后一个参数使用它是undefined可以在早期版本的Javascript被潜在覆盖:

 /* @ Trollface @ */ 
     undefined = 2; 
    /* Happy debuging! */ 

希望我是很清晰。

4
var a; 

typeof a === 'undefined'; // true 
a === undefined; // true 
typeof a === typeof undefined; // true 
typeof a === typeof sdfuwehflj; // true 
10

从 - JQuery_Core_Style_Guidelines

  • 全局变量:
    typeof variable === "undefined"

  • 局部变量:
    variable === undefined

  • 属性:
    object.prop === undefined