文章出處

問題:

alert(typeof(null)); //objectalert(null instanceof Object); //false

答案:

這是由Javascript規范規定的,Null和Object都是javascript中的數據類型。
Null數據類型只有一個值:null。就像undefined數據類型只有一個值:undefined。
問題出在typeof操作符的定義規范,如下:

11.4.3 The typeof OperatorThe production UnaryExpression : typeof UnaryExpression is evaluated as follows:1. Evaluate UnaryExpression.2. If Type(Result(1)) is not Reference, go to step 4.3. If GetBase(Result(1)) is null, return "undefined".4. Call GetValue(Result(1)).5. Return a string determined by Type(Result(4)) according to the following table:Type ResultUndefined "undefined"Null "object"Boolean "boolean"Number "number"String "string"Object (native anddoesn’t implement[[Call]])"object"Object (native andimplements [[Call]])"function"Object (host) Implementation-dependent

可以看到,對于Null類型的值(只有null),規范就是定義返回”object”這個字符串。但是本質上Null和Object不是一個數據類型,null值并不是以Object為原型創建出來的。所以null instanceof Object是false。但從這里也可以看到,null確實是javascript中用來表示空引用的一個特殊值。使得它不是instanceof Ojbect,而typeof null是“object”。在語義上也是可以理解的。

看文倉www.kanwencang.com網友整理上傳,為您提供最全的知識大全,期待您的分享,轉載請注明出處。
歡迎轉載:http://www.kanwencang.com/bangong/20170118/89016.html

文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()