DEV Community

Brandon Roberts
Brandon Roberts

Posted on

Global Object Reflect In JavaScript

As I was reading Axel's book on Generator functions I came across a new Global Object in Javascript that I never even knew existed!
Reflect

22.1.3 Use case: implementing iterables #
The objects returned by generators are iterable; each yield contributes to the sequence of iterated values. Therefore, you can use generators to implement iterables, which can be consumed by various ES6 language mechanisms: for-of loop, spread operator (...), etc.

The following function returns an iterable over the properties of an object, one [key, value] pair per property:

function* objectEntries(obj) {
    const propKeys = Reflect.ownKeys(obj);

    for (const propKey of propKeys) {
        // `yield` returns a value and then pauses
        // the generator. Later, execution continues
        // where it was previously paused.
        yield [propKey, obj[propKey]];
    }
}

This was interesting to me that there was this object available so I decided to start to research this more.

You can learn more about Reflect here.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect

Top comments (0)