2009-04-22 40 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