Last active
January 10, 2022 17:54
-
-
Save slavafomin/c7ab7f7e45431748ad8c to your computer and use it in GitHub Desktop.
JavaScript is Object Empty Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns true if specified object has no properties, | |
* false otherwise. | |
* | |
* @param {object} object | |
* @returns {boolean} | |
*/ | |
function isObjectEmpty(object) | |
{ | |
if ('object' !== typeof object) { | |
throw new Error('Object must be specified.'); | |
} | |
if (null === object) { | |
return true; | |
} | |
if ('undefined' !== Object.keys) { | |
// Using ECMAScript 5 feature. | |
return (0 === Object.keys(object).length); | |
} else { | |
// Using legacy compatibility mode. | |
for (var key in object) { | |
if (object.hasOwnProperty(key)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment