Skip to content

Instantly share code, notes, and snippets.

@formariz
Created May 15, 2014 19:44
Show Gist options
  • Save formariz/57d43b81299a71d1d596 to your computer and use it in GitHub Desktop.
Save formariz/57d43b81299a71d1d596 to your computer and use it in GitHub Desktop.
Object length
Object.prototype.length = function() {
var count = 0, key;
for (key in this) {
if (this.hasOwnProperty(key)) count++;
}
return count;
}
/**
* Example of use
**/
// Create the object
var myObject = new Object({ 100: "a", 28: "b", 71: "c"});
// Alert length of Object
alert(myObject.length()); // return 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment