Is it possible to simulate Object.getOwnPropertyNames in IE8?
I'm trying to get this fiddle to work in IE8.
I believe the only thing left is to make a function simulating getOwnPropertyNames.
Of course other solutions to the base problem of extending a JavaScript object with object literals in IE8 is greatly appreciated.
Updated: Working fiddle which makes use of an external es5 shim script file.
Conclution: No, but you can shim Object.keys
getOwnPropertyNames(a) returns all own properties of the object a . Object. keys(a) returns all enumerable own properties. It means that if you define your object properties without making some of them enumerable: false these two methods will give you the same result.
Object. getOwnPropertyNames() returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly in a given object obj . The ordering of the enumerable properties in the array is consistent with the ordering exposed by a for...in loop (or by Object.
Use object. keys(objectName) method to get access to all the keys of object. Now, we can use indexing like Object. keys(objectName)[0] to get the key of first element of object.
No.
Object.getOwnPropertyNames() returns both enumerable and non-enumerable own properties of an object. It's not possible to iterate over non-enumerable properties in ECMAScript 3rd Edition implementations, so you can only get those that are enumerable.
It's fairly simple to write a procedure to return enumerable own properties:
var arr = [];
for (var k in obj) {
if (obj.hasOwnProperty(k))
arr.push(k);
}
This is (more or less) the equivalent of Object.keys(). If this isn't sufficient, however, then you're out of luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With