Created
February 24, 2017 13:17
-
-
Save sayanriju/20b0e521316d5f8f815607ee5d5b7f2f to your computer and use it in GitHub Desktop.
Hack to test if an Object is "empty"
This file contains hidden or 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
Object.prototype.isEmpty = function () { | |
return !Object.keys(this) | |
.map(k => this.hasOwnProperty(k), this) // bind the value of `this` for each callback | |
.length | |
} | |
// Usage: | |
// > let obj0 = {} | |
// > let obj1 = {foo: 1, bar: 2} | |
// > obj0.isEmpty() // true | |
// > obj1.isEmpty // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment