In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to judge whether the object is empty in JavaScript. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
In your daily development, you usually use Object.keys () to determine whether the object is empty:
Const obj = {}; Object.keys (obj). Length = = 0 / / true is an empty object
But in one article, it was judged like this:
Const obj = {}; Object.keys (obj). Length = = 0 & & obj.constructor = Object
Why judge the constructor of an object in addition? What about the constructor?
Object.keys ()
Introduction to Object.keys () in MDN:
The Object.keys () method returns an array of enumerable properties of a given object.
Example var arr = ['aqu,' baked,'c']; console.log (Object.keys (arr)); / / ['0,'1,'2'] var obj = {0: 'axiom, 1:' baked, 2:'c'}; console.log (Object.keys (obj); / / ['0,'1,'1,'2'] Why do you need an additional constructor to determine the object?
The author of the article wrote that it was to cover the wrapper instance (I was a little confused at the time), which was explained later. In JavaScript, there are nine built-in constructors.
New Object (); new String (); new Number (); new Boolean (); new Array (); new RegExp (); new Function (); new Date (); new Error ()
We can use these constructors to create objects, such as new Object () (not recommended on a daily basis).
Const obj = new Object (); Object.keys (obj). Length = 0; / / true
Is there nothing wrong with the usual method of judgment?
But there is a problem when you create it using the remaining built-in constructors.
Function badEmptyCheck (value) {return Object.keys (value). Length = 0;} badEmptyCheck (new String ()); / / truebadEmptyCheck (new Number ()); / / truebadEmptyCheck (new Boolean ()); / / truebadEmptyCheck (new Array ()); / / truebadEmptyCheck (new RegExp ()); / / truebadEmptyCheck (new Function ()); / / truebadEmptyCheck (new Date ()); / / truebadEmptyCheck (new Error ();); / / true
Don't you get it?
After adding constructor judgment:
Function goodEmptyCheck (value) {return Object.keys (value). Length = = 0 & & value.constructor = Object;} goodEmptyCheck (new String ()); / / falsegoodEmptyCheck (new Number ()); / / falsegoodEmptyCheck (new Boolean ()); / / falsegoodEmptyCheck (new Array ()); / / falsegoodEmptyCheck (new RegExp ()); / / falsegoodEmptyCheck (new Function ()); / / falsegoodEmptyCheck (new Date ()); / / falsegoodEmptyCheck (new Error ();); / / false
The result is correct, in fact, we have dealt with the boundary situation.
Another boundary case, Object.keys (null). Length = 0; / / TypeErrorObject.keys (undefined). Length = 0; / / TypeError
The solution is also simple:
Function goodEmptyCheck (value) {return value & & Object.keys (value). Length = = 0 & & value.constructor = Object;} this is the end of the article on "how to determine whether an object is empty in JavaScript". I hope the above content can be of some help to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.