Last active
December 10, 2015 12:38
-
-
Save gaboesquivel/4435001 to your computer and use it in GitHub Desktop.
Object.getOwnPropertyNames example
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 arr = ["a", "b", "c"]; | |
print(Object.getOwnPropertyNames(arr).sort()); // prints "0,1,2,length" | |
// Array-like object | |
var obj = { 0: "a", 1: "b", 2: "c"}; | |
print(Object.getOwnPropertyNames(obj).sort()); // prints "0,1,2" | |
// Printing property names and values using Array.forEach | |
Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) { | |
print(val + " -> " + obj[val]); | |
}); | |
// prints | |
// 0 -> a | |
// 1 -> b | |
// 2 -> c | |
// non-enumerable property | |
var my_obj = Object.create({}, | |
{ getFoo: { value: function() { return this.foo; }, | |
enumerable: false | |
} | |
}); | |
my_obj.foo = 1; | |
print(Object.getOwnPropertyNames(my_obj).sort()); // prints "foo, getFoo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment