bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript Reflect

Concept visual

JavaScript Reflect

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

The Reflect Object

Formula

Reflect is a object with methods for low - level operations on JavaScript objects.

With the

Reflect object you can get, set, delete, and check object properties in a consistent way.

Reflect was added to JavaScript in

ES6 (2015).

Yor Toolbox

Reflect is a toolbox for working with objects in a safe and consistent way.

Before Reflect

Before Reflect, object operations were scattered:

Using operators like in and delete

Using methods like

Object.defineProperty

Using language mechanisms like

[[

Get

]] and [[

Set

]]

After Reflect

Reflect brings all object operations into clean methods

Reflect methods unifies object operations

Formula

Reflect methods are more predictable than operators (in/delete)

Reflect methods provides standard return values instead of errors

Formula

Reflect methods are cleaner and safer for meta - programming

Reflect methods are tailored for the Proxy object

Examples

With

Reflect.has(), you get the in operator as a function.

With

Reflect.delete(), you get the delete operator as a function.

Why Reflect

Reflect is safe and flexible, especially when used inside a Proxy. Reflect.has()

The

Reflect.has() method checks if an object has a specific property.

The

Reflect.has() method is similar to the in operator.

Check if Property Exists

// Create an Object const person = {name: "John", lastname: "Doe"};

let answer = Reflect.has(person, "name");

Same as using the in operator:

let answer = "name" in person;

Syntax

Reflect.has(obj, prop)

Formula

Properties obj - the target object prop - the property to check
Returns true - if true false if false

Exeptions

TypeError thrown if obj is not an object Reflect.deleteProperty()

The

Reflect.deleteProperty() method deletes a property from an object.

The

Reflect.deleteProperty() method is similar to the delete operator.

Deleting a Property

// Create an Object const person = {name: "John", lastname: "Doe"};

Reflect.deleteProperty(person, "name");

Same as using the delete operator:

delete person.name;

Previous

JavaScript Meta Programming

Next

JavaScript Proxy