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

Source: Internet
Author: User
Tags null null readable hasownproperty

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.

var//"undefined"null; alert (typeof//" Object "null//truebj =null//False 

The following function detects if the object is empty:

/* * Detects if the 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 prototype (and therefore does not make hasownproperty).  */function  isEmpty (obj)    {for (var in  obj) {         return false ;    }     return true ;};    

Whether the empty object here is {} or null. Write a test case below.

var a == ' realwall '//false//trueconsole.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 Statement

The following method determines whether an object is empty, detects only the properties of the object itself, and does not detect attributes inherited from the prototype.

/* * Detects if the object is an empty object (does not contain any readable properties). The * method only detects properties of the object itself and does not detect attributes inherited from the prototype.  */function  isownempty (obj)    {for (var in obj) {         if (Obj.hasownproperty (name)) {            returnfalse;        }    }     return true ;};            

The difference between {} and null:

var a = {}; var NULL  = ' realwall '// error here, B is a null pointer object and cannot be added directly as a normal object. B =// at this point A 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

Related Article

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.