Created
March 26, 2012 03:12
-
-
Save ethertank/2202606 to your computer and use it in GitHub Desktop.
Object.length
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
Object.prototype.length || (Object.prototype.length = function() { | |
var len = -1; | |
for (prop in this) { | |
len++; | |
} | |
return len; | |
}); |
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
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