Skip to content

Instantly share code, notes, and snippets.

@gaboesquivel
Last active December 10, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaboesquivel/4435001 to your computer and use it in GitHub Desktop.
Save gaboesquivel/4435001 to your computer and use it in GitHub Desktop.
Object.getOwnPropertyNames example
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