Home

Awesome

NPM version Dependencies

This is a shim for the ECMAScript 6 Reflect and Proxy objects.

This library does two things:

July 2016 update: the most recent version of all major browsers and node.js now support fully ES6-compliant Reflect and Proxy objects. This shim is primarily useful if you want ES6 Reflect support on older browsers or versions of node.js < v6.0.0, or if you want ES6 Proxy support on versions of node.js < v6.0.0.

May 2016 update: the recently released V8 v4.9 includes native support for ES2015 Proxies and Reflect, making this library obsolete for environments that embed V8 4.9 or newer (like Chrome 49 and Node v6.0). Node v5.10.x or lower still requires this polyfill for proper ES6 Proxy support.

Read Why should I use this library?

Installation

For node.js, install via npm:

npm install harmony-reflect

Then:

node --harmony-proxies
> var Reflect = require('harmony-reflect');

See release notes for changes to the npm releases.

To use in a browser, just download the single reflect.js file. After loading

<script src="reflect.js"></script>

a global object Reflect is defined that contains reflection methods as defined in the ES6 spec.

This library also updates the "harmony-era" Proxy object in the V8 engine (also used in node.js) to follow the latest ECMAScript 2015 spec. To create such a proxy, call:

var proxy = new Proxy(target, handler);

See below for a list of spec incompatibilities and other gotcha's.

API Docs

This module exports an object named Reflect and updates the global Proxy object (if it exists) to be compatible with the latest ECMAScript 6 spec.

The ECMAScript 6 Proxy API allows one to intercept various operations on Javascript objects.

Compatibility

The Reflect API, with support for proxies, was tested on:

If you need only Reflect and not an up-to-date Proxy object, this library should work on any modern ES5 engine (including all browsers).

Compatibility notes:

Dependencies

After loading reflect.js into your page or other JS environment, be aware that the following globals are patched to be able to recognize emulated direct proxies:

Object.getOwnPropertyDescriptor
Object.defineProperty
Object.defineProperties
Object.getOwnPropertyNames
Object.getOwnPropertySymbols
Object.keys
Object.{get,set}PrototypeOf
Object.assign
Object.{freeze,seal,preventExtensions}
Object.{isFrozen,isSealed,isExtensible}
Object.prototype.valueOf
Object.prototype.isPrototypeOf
Object.prototype.toString
Object.prototype.hasOwnProperty
Function.prototype.toString
Date.prototype.toString
Array.isArray
Array.prototype.concat
Proxy
Reflect

:warning: In node.js, when you require('harmony-reflect'), only the current module's globals are patched. If you pass an emulated direct proxy to an external module, and that module uses the unpatched globals, the module may not interact with the proxy according to the latest ES6 Proxy API, instead falling back on the old pre-ES6 Proxy API. This can cause bugs, e.g. the built-in Array.isArray will return false when passed a proxy-for-array, while the patched Array.isArray will return true. I know of no good fix to reliably patch the globals for all node modules. If you do, let me know.

Examples

The examples directory contains a number of examples demonstrating the use of proxies:

Other example uses of proxies (not done by me, but using this library):

For more examples of proxies, and a good overview of their design rationale, I recommend reading Axel Rauschmayer's blog post on proxies.

Proxy Handler API

The sister project proxy-handlers defines a number of predefined Proxy handlers as "abstract classes" that your code can "subclass" The goal is to minimize the number of traps that your proxy handlers must implement.

Spec Incompatibilities and other gotcha's

This library differs from the ECMAScript 2016 spec as follows:

This library differs from the ECMAScript 2015 spec as follows: