Javascript/Jquery method for determining whether an object is null _ javascript skills

Source: Internet
Author: User
This article will share with you the method of JSJquery to determine whether an object is null. it is very simple and practical. if you need it, you can refer to it. A clever implementation is found: check whether an Object is null, that is, it does not contain any element. Objects in Javascript are a dictionary that contains a series of Key Value pairs (Key Value Pair ). Check whether an object is null. it is equivalent to checking whether the object has a key-value pair. Write code, such:

If (isEmptyObject (obj) {// obj is empty} else {// not empty}

As for the implementation of isEmptyObject, jQuery has a very idea. please refer to the code:

Function isEmptyObject (obj) {for (var key in obj) {return false;} return true ;}

Although Javascript does not provide native isEmpty () methods, it provides an iterator that can be used to traverse all key-value pairs. So what jQuery does is to try to traverse. if there is any key-value pair, that is, the object is not empty, and false is returned directly. In terms of efficiency, because only one element is read, at most some overhead of jumping out of the loop is added, the actual performance will not be much worse than that of the native method.

Function isNullObj (obj) {for (var I in obj) {if (obj. hasOwnProperty (I) {return false ;}} return true ;}

The above is all the content of this article. I hope you will like it.

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.