Skip to content

Instantly share code, notes, and snippets.

@sayanriju
Created February 24, 2017 13:17
Show Gist options
  • Save sayanriju/20b0e521316d5f8f815607ee5d5b7f2f to your computer and use it in GitHub Desktop.
Save sayanriju/20b0e521316d5f8f815607ee5d5b7f2f to your computer and use it in GitHub Desktop.
Hack to test if an Object is "empty"
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