Sorry but still not exact, the use of Object.getOwnPropertyNames through 
prototype chain can result in duplicate keys in override cases, so if we 
implement this API ourself it could be quite complex (involving a Map to filter 
duplicated keys)

I’m just wondering is there any reason that Reflect API is not suitable to 
provide such functionality?



Best regards

Gray Zhang



在 2015年6月1日 下午12:52:26, Fink Steve ([email protected]) 写到:

Forgive me for golfing it, but

function getAllPropertyNames(o) {
    if (!o) return [];
    return Object.getOwnPropertyNames(o) + 
getAllPropertyNames(Object.getPrototypeOf(o));
}

or as a generator

function* allPropertyNames(o) {
    if (!o) return;
    yield* Object.getOwnPropertyNames(o);
    yield* allPropertyNames(Object.getPrototypeOf(o));
}

don't seem too onerous.

Though on the other hand, didn't I hear that prototype loops are now possible 
with Proxies? If so, then you'd need to handle that.

Then again, if you're going to handle weird cases, then what should it even 
return if you go through a Proxy's getPrototypeOf trap that mutates the set of 
properties?

On 05/31/2015 04:42 AM, Gray Zhang wrote:
Since class’s members are non-enumerable by default (which is a good choice) we 
cannot use for .. in to iterate over all members of an instance, the same 
problem could exists in a plain object when we use Object.defineProperty API.

In real world there are some scenarios where we need to iterate over members, A 
common example is we need to find all set{SomeThing} methods so we can do an 
auto dependency injection.

Certainly we can write a 3rd-party function to find all members through 
prototype chain:

function getAllMembersKeys(obj) {
    let keys = [];
      
    while (obj) {
        keys.push(...Object.getOwnPropertyNames(obj));
        obj = Object.getPrototypeOf(obj);
    }
      
    return keys;
}

But it doesn’t look nice and lacks considerations of many things such as 
Symbol’d keys.

Look around other languages with reflection API, most of them would provide a 
method to iterate over all members / properties / methods of an instance, so 
why not we provide a set of utility API:

Reflect.getAllMembersNames
Reflect.getAllMemberDescriptors
Reflect.getAllMethodNames
Reflect.getAllMethodDescriptors
Reflect.getAllPropertyNames
Reflect.getAllPropertyDescriptors


Best regards

Gray Zhang




_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________  
es-discuss mailing list  
[email protected]  
https://mail.mozilla.org/listinfo/es-discuss  
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to