Rapid ES6 Training: The Reflect API

mark-zamoyta-v1

Welcome to Part 6 of this review of the Pluralsight course Rapid ES6 Training by Mark Zamoyta.

Mark started in the developer world over 25 years ago.

He has written numerous mobile apps and games for clients including Electronic Arts and Yahoo! Games.

Rapid ES6 Training is the final course in the new JavaScript learning path.

The aim of the course is for you to be able to use the new syntax and features of ES6, and the prerequisite is to be familiar with the earlier ES5 version of JavaScript.

The Reflect API

Construction and Method Calls

Mark begins by explaining that Reflect is an object:

console.log(typeof Reflect)

There are many examples explaining the construct method on this object.

Reflect.construct(target, argumentsList[, newTarget])

See Reflect.construct() on MDN

Then Mark explains the apply function:

Reflect.apply(target, thisArgument,  argumentsList)

See Reflect.apply() on MDN

See Reflect on MDN

Reflect and Prototypes

We learn about the getPrototypeOf method:

Reflect.getPrototypeOf(target)

See Reflect.getPrototypeOf() on MDN

Mark also teaches us setPrototypeOf:

Reflect.setPrototypeOf(target, prototype)

See Reflect.setPrototypeOf() on MDN

Reflect and Properties

The most commonly used property is get:

Reflect.get(target, propertyKey[, receiver])

We pass in the object and the value of the property name that we want to get.

In this following example we are shown us that the third argument is the value of this within the function:

class Restaurant {
    constructor() {
        this._id = 9000;
    }
    get id() {
        return this._id;
    }
}

let r = new Restaurant();
console.log(Reflect.get(r, 'id', { _id: 88 }));

See Reflect.get() on MDN

Next Mark teaches us about the set method:

Reflect.set(target, propertyKey, value[, receiver])

See Reflect.set() on MDN

We also learn the has method:

Reflect.has(target, propertyKey)

See Reflect.has() on MDN

There’s more! ownKeys is explained next:

Reflect.ownKeys(target)

See Reflect.ownKeys() on MDN

Mark also gives us an example of defineProperty:

Reflect.defineProperty(target, propertyKey, attribute)

See Reflect.defineProperty() on MDN

There’s also deleteProperty:

Reflect.deleteProperty(target, propertyKey)

See Reflect.deleteProperty() on MDN

Finally there’s getOwnPropertyDescriptor:

getOwnPropertyDescriptor(target, propertyKey)

See Reflect.getOwnPropertyDescriptor() on MDN

Reflect and Property Extensions

Sometimes we might want to prevent properties being added to an object. Mark explains each of the functions for working with property extensions:

Reflect.preventExtensions(target)

This pretty much does “what it says on the tin” as the saying goes.

See Reflect.preventExtensions() on MDN

isExtensible is also pretty intuitive, return true or false.

Reflect.isExtensible(target)

See Reflect.isExtensible() on MDN

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s