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.