Skip to content

Instantly share code, notes, and snippets.

@ethertank
Created March 26, 2012 03:12
Show Gist options
  • Save ethertank/2202606 to your computer and use it in GitHub Desktop.
Save ethertank/2202606 to your computer and use it in GitHub Desktop.
Object.length
Object.prototype.length || (Object.prototype.length = function() {
var len = -1;
for (prop in this) {
len++;
}
return len;
});
var obj_1 = { a: "aaa", b: "bbb", c: 11111 },
obj_2 = new Object();
alert(obj_1.length()); //3
alert(obj_2.length());​ //0​​​​​​​​
// ※空でも一回 for in が回ってしまうみたいなので -1 からスタートしてる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment