Javascript confusing syntax inconsistence for null, instanceof and typeof?

前端 未结 3 2015
执念已碎
执念已碎 2020-12-20 08:16
var obj = {};
typeof obj; // returns \"object\"
obj instanceof Object // return true

typeof null // returns \"object\"
null instanceof Object // returns false
         


        
3条回答
  •  [愿得一人]
    2020-12-20 08:43

    instanceof more appealing, however you checked instaceof undefined going throw exception that because the undefined is not any singular type object name, It's may or may not sub method of JavaScript Object. Because undefined that type for null (Null Type).

    typeof undefined // return "undefined"
    undefined instanceof Object // return False 
    

    You should check undefined is instaceof JS object, that give a no it's not the object. It's give a boolean result.

提交回复
热议问题