http://blog.csdn.net/yiluoak_47/article/details/7766760
First, the difference between null and undefined:
Executes typeof on declared but uninitialized and undeclared variables, returning "undefined".
Null represents an empty object pointer, and the typeof operation returns "Object".
Generally do not explicitly set the value of the variable to undefined, but null instead, for the variable that will save the object, you should explicitly let the variable hold a null value.
1 varBJ;2 alert (BJ);3 //"undefined"4 BJ=NULL;6AlerttypeofBJ); //"Object"8 Alert (BJ==NULL); //true One BJ= {}; - Alert (BJ==NULL); //false
The following two functions are given to me by Brother Deng Shi, thank you.
1 /*2 *3 detects whether the object is an empty object (does not contain any readable properties). 4 *5 The hasOwnProperty method detects both the properties of the object itself and the attributes inherited from the prototype (and therefore does not make the. 6 */7 functionisEmpty (obj)8 {9 for(varname inchobj) One { A return false; - } - return true; the};
Whether the empty object here is {} or null. I have written a test case.
1 vara= {};3 A.name= ' Realwall ';5 Console.log (IsEmpty (a));//false7 Console.log (IsEmpty ({}));//true9Console.log (IsEmpty (NULL)); //true //Note there is no syntax error when the parameter is null Oh, that is, although you cannot add a property to a NULL Null pointer object, you can use the forinchstatement. - the? - /* - * - detects whether the object is an empty object (does not contain any readable properties). + * - method only detects properties of the object itself and does not detect attributes inherited from the prototype. + */ A functionisownempty (obj) at { - for(varname - inchobj) - { - if(Obj.hasownproperty (name)) - { in return false; - } to } + return true; - }; the * the difference between {} and null: $ this thing is very important. Panax Notoginseng? - vara= {}; + varb=NULL; the + A.name= ' Realwall '; $ B.name= ' Jim '; - //there will be an error, B is a null pointer object and cannot be directly added as a normal object. - b=A; - B.nameb= ' Jim '; the //at thisA and B point to the same object. A.name, b.name are ' jam '
Determine if the JavaScript object is null or the property is empty