Simple method to determine whether a JavaScript object is null or the attribute is empty

Source: Internet
Author: User
Tags null null readable hasownproperty

First of all, the difference between null and undefined:

Performs typeof on declared but uninitialized and undeclared variables, and returns "undefined".

Null represents an empty object pointer, and the typeof operation returns "Object".

It is generally not explicit to set the value of the variable to undefined, but null instead, for the variable that will save the object, make it clear that the variable holds the null value.

 
1 2 3 4 5 6 7 var bj; Alert (BJ); "undefined" BJ = null; Alert (typeof BJ); "Object" alert (BJ = = null); True BJ = {}; Alert (BJ = = null); False

The following two functions are Deng Shi brother to me, thank you ah.

 
1 2 3 4 5 6 7 8 9 10 11-12 * * Detects if an object is an empty object (does not contain any readable properties). The * method detects both the properties of the object itself and the attributes inherited from the stereotype (and therefore does not make hasownproperty). */function IsEmpty (obj) {for (var name in obj) {return false;} return true;};

The empty object, in the end, is {} or null. I wrote a test case.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24-25 var a = {}; A.name = ' Realwall '; Console.log (IsEmpty (a)); False Console.log (IsEmpty ({})); True Console.log (IsEmpty (null)); True//Note No syntax error when parameter is null Oh, that is, although you cannot add a property to a NULL Null pointer object, you can use the for in statement? * * Detects if an object is an empty object (does not contain any readable properties). The * method detects only the properties of the object itself and does not detect inherited properties from the stereotype. */function Isownempty (obj) {for (var name in obj) {if (Obj.hasownproperty (name)) {return false;}} return true;

{} differs from null:

This thing is very important.

var a = {}; var b = null;    a.name = ' Realwall '; B.name = ' Jim '; This will be an error, B is a null pointer object, you cannot add attributes directly like normal objects. b = A; B.name = ' Jim '; A and B point to the same object at this point. A.name, b.name are ' jam '

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.