Determine if the JavaScript object is null or the property is empty

Source: Internet
Author: User
Tags null null hasownproperty

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.