JavaScript - How to list properties of an object?

189 views Asked by At

Is there a way to list all properties of an object (which may be even a function) including non-enumerable ones, but without using Object.getOwnPropertyNames or other methods of Object?

So, for example, I need to get all properties of native String constructor (fromCharCode, fromCodePoint, raw, etc), but I'm not allowed to use native function from Object called getOwnPropertyNames. Is there a way to achieve that?

I've tried for..in and for..of loops, but not all properties are listed. Also, I've tried Object.keys, but again, not all properties are listed. Also, to be make clear what I'm asking for: the following is not allowed:

var a = Object.getOwnPropertyNames.bind(Object);
console.log(a(String));

I'm asking this because I'm playing some game (called ElevatorJS) where a user writes a JavaScript program which should achieve something in restricted conditions. In this particular condition, the method getOwnPropertyNames is overriden and is equal null, so we are not allowed to use it. But, it turned out that they've hidden some non-enumerable property inside String constructor as a property, so I have to access it somehow. Also, they've overriden other methods of Object too (like entries, getOwnPropertyDescriptors, etc).

Is there a way to achieve that? Thank you in advance.

1

There are 1 answers

2
spanky On

You can use Reflect.ownKeys(String), which will give you all the owned members of the argument.