Tips for determining whether a JavaScript Object is null or its property is null _ javascript

Source: Internet
Author: User
Tags hasownproperty
When typeof is executed on declared but uninitialized and undeclared variables, undefined is returned. null indicates a null object Pointer. The typeof operation will return the object. First, the difference between null and undefined is described as follows:

If typeof is executed on declared but uninitialized and undeclared variables, "undefined" is returned ".

Null indicates a null object Pointer. The typeof operation returns "object ".

Generally, the value of a variable is not explicitly set to undefined, but the opposite is null. For a variable that will save the object, the variable should be explicitly set to save the null value.

var bj;alert(bj); //"undefined"bj = null;alert(typeof bj); //"object"alert(bj == null); //truebj = {};alert(bj == null); //false

The following two functions are provided by brother Deng. Thank you.

/** Check whether the object is a null object (excluding any readable attributes ). * This method detects both attributes of an object and attributes inherited from the prototype (hasOwnProperty is not used ). */Function isEmpty (obj) {for (var name in obj) {return false;} return true ;};

Whether the empty object is {} or null. I wrote a test case.

Var a = {};. name = 'realwall'; console. log (isEmpty (a); // falseconsole. log (isEmpty ({}); // trueconsole. log (isEmpty (null); // true // when the parameter is null, no syntax error occurs. That is, although null pointer objects cannot be added, but can I use the for in statement? /** Check whether the object is a null object (excluding any readable attributes ). * The method only detects the attributes of the object and does not detect the attributes inherited from the prototype. */Function isOwnEmpty (obj) {for (var name in obj) {if (obj. hasOwnProperty (name) {return false ;}} return true ;};

Difference between {} and null:

This is very important.

Var a = {}; var B = null;. name = 'realwall'; B. name = 'Jim '; // an error is reported here. B is a null pointer object and cannot directly add attributes like a common object. B = a; B. name = 'Jim '; // At this time, a and B point to the same object. A. name and B. name are both 'jam'
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.