JavaScript: Reflect Object and Properties (ES2015)

By Xah Lee. Date: . Last updated: .

The Reflect object is used as namespace to host a group of static functions. (like the Math object. 〔➤see JavaScript: Math Object and Properties〕 ) These functions are operators in function form, or, as variations to some static methods in Object and Function objects.

// type of Reflect
console.log ( typeof Reflect === "object" ); // true
console.log ( Object.prototype.toString.call(Reflect) === "[object Object]" ) // true

The parent of Reflect is Object.prototype. 〔➤see JavaScript: Prototype and Inheritance〕 〔➤see JavaScript: Object.prototype Properties

console.log ( Reflect.getPrototypeOf ( Reflect ) === Object.prototype); // true

Properties


Reflect.get

Reflect.get ( obj, key , receiver )

Similar to obj[key].

Get the value of a property, own property or in prototype chain.

If key is not found, return value is undefined.

If obj is not object, throw a TypeError exception.

receiver todo

var u = {p:3};
console.log( Reflect.get ( u , "p" ) ); // 3
console.log( Reflect.get ( u , "h" ) ); // undefined
// Reflect.get will traverse the prototype chain to find property
var u = {p:3}
var v = Object.create (u)
console.log ( Reflect.get ( v, "p" ) ) // 3

ECMAScript 2015 §Reflection#sec-reflect.get

Reflect.set

Reflect.set ( obj, key, val, receiver )

Similar to obj[key] = val.

Add or modify a property. Returns true if success, else false.

receiver todo

var e = {};
Reflect.set ( e , "p", 3 )
console.log(e) // { p: 3 }

Example of failure:

// example of failed attempt to set property
var e = {};
Object.preventExtensions ( e );
var returnValue = Reflect.set ( e , "p", 3 );
console.log(e) // {}
console.log(returnValue) // false

ECMAScript 2015 §Reflection#sec-reflect.set

Reflect.has

Reflect.has ( obj, key )

Similar to key in obj.

Returns true if obj has property key key or in prototype chain. Else false.

var u = {p:3}
console.log( Reflect.has ( u , "p" ) ) // true
console.log( Reflect.has ( u , "y" ) ) // false
// Reflect.has return true if property is own property or in prototype chain
var u = {p:3}
var v = Object.create (u)
console.log( Reflect.has ( v , "p" ) ) // true

ECMAScript 2015 §Reflection#sec-reflect.has


Reflect.defineProperty

Reflect.defineProperty ( obj, key, attributes )

Create a property with specified attribute values, or modify a existing property's attributes. Returns true if successful, else false.

Similar to Object.defineProperty(obj, key, attributes), except the Reflect version returns true or false. (not object or error) 〔➤see JavaScript: Property Attributes: Writable, Enumerable, Configurable

// using Object.defineProperty

var x = {};

// define a property and assign its value and attributes
Object.defineProperty(x, "p",
{ value : 3, writable: true, enumerable: false, configurable: true}
);

console.log(x["p"]);            // 3
var u = {};
var result = Reflect.defineProperty(u, "p", { value : 3, writable: true, enumerable: true, configurable: true });
console.log( u ) // { p: 3 }
console.log( result ) // true

ECMAScript 2015 §Reflection#sec-reflect.defineproperty

Reflect.deleteProperty

Reflect.deleteProperty ( obj, key )

Deletes the property key from obj.

Returns true if successful, else false.

Similar to delete obj[key].

var obj = {x: 1, y: 2};
Reflect.deleteProperty ( obj , "y" );
console.log(obj); // { x: 1 }
var u = {p: 1}
Object.freeze ( u )
console.log ( Reflect.deleteProperty ( u , "p" ) ) // false

Note, delete expr just return true if expr is not a object.

console.log ( delete 3 ) // true

ECMAScript 2015 §Reflection#sec-reflect.deleteproperty

Reflect.getOwnPropertyDescriptor

Reflect.getOwnPropertyDescriptor ( obj, key )

Returns the property attributes. If the property doesn't exist, returns undefined.

If obj is not object, throw a TypeError exception.

Note: Object.getOwnPropertyDescriptor ( obj, key ) will first convert obj to object.

〔➤see JavaScript: Property Attributes: Writable, Enumerable, Configurable

var h = {p:4}
console.log ( Object.getOwnPropertyDescriptor ( h, "p" ) )
console.log ( Reflect.getOwnPropertyDescriptor ( h , "p" ) )
// both prints
// { value: 4, writable: true, enumerable: true, configurable: true }

// non-exist key
console.log ( Object.getOwnPropertyDescriptor ( h, "yyy" ) ) // undefined
console.log ( Reflect.getOwnPropertyDescriptor ( h , "yyy" ) ) // undefined

ECMAScript 2015 §Reflection#sec-reflect.getownpropertydescriptor

ECMAScript 2015 §Fundamental Objects#sec-object.getownpropertydescriptor


Reflect.ownKeys

Reflect.ownKeys ( obj )

Return a array of the object's keys. (including symbol keys and non-enumerable keys. 〔➤see JavaScript: Symbol (ES2015)〕 )

var u = {p1:1, p2:2}
var r = Reflect.ownKeys ( u )
console.log ( r ) // [ 'p1', 'p2' ]
console.log ( Array.isArray ( r ) ) // true

Note: Object.getOwnPropertyNames (obj) return only properties that are string keyed.

Note: Object.keys(obj) return only properties that are string keyed and enumerable.

ECMAScript 2015 §Reflection#sec-reflect.ownkeys

ECMAScript 2015 §ECMAScript Data Types and Values#sec-object-internal-methods-and-internal-slots

ECMAScript 2015 §Fundamental Objects#sec-object.keys

Reflect.enumerate

Reflect.enumerate ( obj )

Return a iterator object of the obj's property keys that are string keyed and enumerable.

Note: This function has been removed in ES7. It is not implemented in browsers.

Use Object.keys(obj) instead.

ECMAScript 2015 §Reflection#sec-reflect.enumerate


Reflect.getPrototypeOf

Reflect.getPrototypeOf ( obj )

Returns the prototype (aka parent) object. (return null if no parent.)

Throw a TypeError exception if obj is not a object.

Note: Object.getPrototypeOf ( obj ) will convert obj to a object first.

console.log ( Reflect.getPrototypeOf ( Array ) ) // [Function]
console.log ( Object.getPrototypeOf ( Array ) ) // [Function]

console.log (
 Reflect.getPrototypeOf ( Array ) === Object.getPrototypeOf ( Array )
) // true
var h = Object.create (null)
console.log ( Reflect.getPrototypeOf ( h ) ) // null

ECMAScript 2015 §Reflection#sec-reflect.getprototypeof

ECMAScript 2015 §Fundamental Objects#sec-object.getprototypeof

Reflect.setPrototypeOf

Reflect.setPrototypeOf ( obj, proto )

Sets the parent object obj to proto.

if obj is not a object throw a TypeError exception.

if proto is not a object nor null throw a TypeError exception.

var a = {a1:1}
var b = {b1:1}
console.log ( Reflect.getPrototypeOf ( a ) ) // {}
console.log ( Reflect.setPrototypeOf ( a, b ) ) // true
console.log ( Reflect.getPrototypeOf ( a ) ) // { b1: 1 }

ECMAScript 2015 §Reflection#sec-reflect.setprototypeof


Reflect.isExtensible

Reflect.isExtensible (obj)

Returns true if obj is extensible. Else, false.

If obj is not object, throw a TypeError exception.

Note: Object.isExtensible(obj) will return false if obj is not a object.

〔➤see JavaScript: Prevent Adding/Deleting/Modifying Object Properties

var u = {}
console.log ( Reflect.isExtensible ( u ) ) // true
Reflect.preventExtensions ( u )
console.log ( Reflect.isExtensible ( u ) ) // false

ECMAScript 2015 §Reflection#sec-reflect.isextensible

ECMAScript 2015 §Fundamental Objects#sec-object.isextensible

Reflect.preventExtensions

Reflect.preventExtensions ( obj )

Sets obj's extensible attribute to false.

Returns true if success, else false.

If obj is not object, throw a TypeError exception.

Note: Object.preventExtensions(obj) returns obj if obj is not a object.

var u = {}
console.log ( Reflect.isExtensible ( u ) ) // true
Reflect.preventExtensions ( u )
console.log ( Reflect.isExtensible ( u ) ) // false

ECMAScript 2015 §Reflection#sec-reflect.preventextensions

ECMAScript 2015 §Fundamental Objects#sec-object.preventextensions

Reflect.apply

Reflect.apply ( f, this_binding, argList )

Returns f() with f's this keyword having value of this_binding, and with argument of argList.

// example of Reflect.apply
function f(a, b) { return 3 + 4; }
console.log( Reflect.apply ( f , undefined, [3,4] ) ); // 7

Here's example with thisBinding:

// example of Reflect.apply with thisBinding argument

function g (a, b) {
    this.x = a;
    this.b = b;
}

var obj = {};

Reflect.apply ( g , obj, [3,4] );

console.log( obj ); // { x: 3, b: 4 }

Reflect.apply( is similar to Function.prototype.apply, but with shorter syntax and easier to understand.

〔➤see JavaScript: f.call f.apply f.bind

argList is array object or array-like object.

ECMAScript 2015 §Reflection#sec-reflect.apply

Reflect.construct

Reflect.construct ( constructor, argList [, newTarget] )

Reflect.construct ( C, [a1, a2, …]) is equivalent to new C(a1, a2, …)

var a = new Array(1,2,3);
var b = Reflect.construct ( Array, [1,2,3] );
console.log(a); // [ 1, 2, 3 ]
console.log(b); // [ 1, 2, 3 ]

todo Reflect.construct ( target, argList, newTarget)

todo new.target

Reference

ES2015 §Reflection#sec-reflect-object

Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.