Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to simulated Object.getOwnPropertyNames in IE8

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

like image 497
Farmor Avatar asked Nov 23 '11 10:11

Farmor


People also ask

What is the difference between object keys and object getOwnPropertyNames?

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.

What is object getOwnPropertyNames?

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.

How do I find the key name of an 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.


1 Answers

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.

like image 188
Andy E Avatar answered Oct 06 '22 00:10

Andy E