Maybe we need a reflect API to iterate over instance members

Gray Zhang otakustay at icloud.com
Sun May 31 17:23:51 UTC 2015


Not exactly

Reflect.ownKeys does not walk up prototype chain so it does not return inherited members
Reflect.enumerate seems not return non-enumerable members
What I propose is an API that returns both non-enumerable and inherited members



Best regards

Gray Zhang



在 2015年6月1日 上午1:15:58, Zhang,Lili(PAPE) (zhanglili01 at baidu.com) 写到:

Not exactly

Reflect.ownKeys does not walk up prototype chain so it does not return inherited members
Reflect.enumerate seems not return non-enumerable members
What I propose is an API that returns both non-enumerable and inherited members



UBFT 张立理
Hi int08h ☎ 18621600455


在 2015年6月1日 上午12:44:52, Harband Jordan (ljharb at gmail.com) 写到:

Would `Reflect.ownKeys` or `Reflect.enumerate` help you here?

On Sun, May 31, 2015 at 4:42 AM, Gray Zhang <otakustay at icloud.com> 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
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150601/1a51d1d0/attachment-0001.html>


More information about the es-discuss mailing list