-
-
Save x404/dfa3e439214fe4c9f27f693d99fc696a 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