JS to determine if it is an empty object

Source: Internet
Author: User

Main ideas

We have to consider the main: JS native object, the host object (above the browser).

First, for the host object

The main judgment is that the DOM object and whether it is a Window object

Whether the DOM object Value.nodetype exists.

is the Window object, value! = NULL && value = = = Value.window;

For both of these are definitely not empty objects.

if (Value.nodetype | | IsWindow (value)) {return false;}


For JS native objects

Whether or not the return is false for itself to return false has ' false ', ' null ', ' "', ' ' undefined ', ' NaN ', ' 0 ' in this 6 case

These obviously cannot be empty objects.

Then determine whether an object is using Object.prototype.toString.call (value) = = = ' [Object Object] '; Abbreviation for IsObject

There is also a judgment to determine whether you have a Hasownprototype method.

if (!value | |!isobject (value) | |!value.hasownprototype) {return false;}

If none of these conditions are met,

You need to handle whether you have your own properties,

for (var key in value) {if (Value.hasownprototype (key)) {return false; }}


Full code:

function Isemptyobject (value) {if (Value.nodetype | | IsWindow (value)) {return false;    } if (!value | |!isobject (value) | |!value.hasownprototype) {return false;        } for (var key in value) {if (Value.hasownprototype (key)) {return false; }} return true;



JS to determine if it is an empty object

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.