source: frontend/node_modules/harmony-reflect/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 9.0 KB
Line 
1[![NPM version](https://badge.fury.io/js/harmony-reflect.svg)](http://badge.fury.io/js/harmony-reflect) [![Dependencies](https://david-dm.org/tvcutsem/harmony-reflect.png)](https://david-dm.org/tvcutsem/harmony-reflect)
2
3This is a shim for the ECMAScript 6 [Reflect](http://www.ecma-international.org/ecma-262/6.0/#sec-reflect-object) and [Proxy](http://www.ecma-international.org/ecma-262/6.0/#sec-proxy-objects) objects.
4
5This library does two things:
6
7 - It defines an ES6-compliant `Reflect` global object that exports the ECMAScript 6 reflection API.
8 - If harmony-era (pre-ES6) `Proxy` support is available, it patches `Proxy` to be up-to-date with the [ES6 spec](http://www.ecma-international.org/ecma-262/6.0/).
9
10**July 2016 update**: the most recent version of all major browsers and node.js now [support fully
11ES6-compliant](http://kangax.github.io/compat-table/es6/#test-Proxy) `Reflect` and `Proxy` objects. This shim is primarily useful if you want ES6 `Reflect`
12support on older browsers or versions of node.js < v6.0.0, or if you want
13ES6 `Proxy` support on versions of node.js < v6.0.0.
14
15**May 2016 update**: the recently released [V8 v4.9](http://v8project.blogspot.com.au/2016/01/v8-release-49.html) 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.
16
17Read [Why should I use this library?](https://github.com/tvcutsem/harmony-reflect/wiki)
18
19Installation
20============
21
22For node.js, install via [npm](http://npmjs.org):
23
24 npm install harmony-reflect
25
26Then:
27
28 node --harmony-proxies
29 > var Reflect = require('harmony-reflect');
30
31See [release notes](https://github.com/tvcutsem/harmony-reflect/blob/master/RELNOTES.md) for changes to the npm releases.
32
33To use in a browser, just download the single reflect.js file. After loading
34
35 <script src="reflect.js"></script>
36
37a global object `Reflect` is defined that contains reflection methods as defined in the [ES6 spec](http://www.ecma-international.org/ecma-262/6.0/#sec-reflect-object).
38
39This library also updates the "harmony-era" `Proxy` object in the V8 engine
40(also used in node.js) to follow the latest [ECMAScript 2015](http://www.ecma-international.org/ecma-262/6.0/) spec.
41To create such a proxy, call:
42
43 var proxy = new Proxy(target, handler);
44
45See below for a list of spec incompatibilities and other gotcha's.
46
47API Docs
48========
49
50This module exports an object named `Reflect` and updates the global `Proxy` object (if it exists) to be compatible with the latest ECMAScript 6 spec.
51
52The ECMAScript 6 Proxy API allows one to intercept various operations on Javascript objects.
53
54 * Overview of all [supported traps](https://github.com/tvcutsem/harmony-reflect/tree/master/doc/traps.md) on proxies
55 * The [Reflect API](https://github.com/tvcutsem/harmony-reflect/tree/master/doc/api.md)
56 * The Proxy [Handler API](https://github.com/tvcutsem/harmony-reflect/tree/master/doc/handler_api.md)
57
58Compatibility
59=============
60
61The `Reflect` API, with support for proxies, was tested on:
62
63 * Firefox (>= v4.0)
64 * `node --harmony_proxies` (>= v0.7.8)
65 * `iojs --harmony_proxies` (>= 2.3.0)
66 * `v8 --harmony_proxies` (>= v3.6)
67 * Any recent `js` spidermonkey shell
68
69If you need only `Reflect` and not an up-to-date `Proxy` object, this
70library should work on any modern ES5 engine (including all browsers).
71
72Compatibility notes:
73
74 * Chrome (>= v19 && <= v37) used to support proxies behind a flag
75 (`chrome://flags/#enable-javascript-harmony`) but Chrome v38 [removed](https://code.google.com/p/v8/issues/detail?id=1543#c44) the `Proxy` constructor. As a result, this library cannot patch the harmony-era `Proxy` object on Chrome v38 or above. If you're working with Chromium directly, it's still possible to enable proxies using `chromium-browser --js-flags="--harmony_proxies"`.
76 * In older versions of V8, the `Proxy` constructor was enabled by
77 default when starting V8 with `--harmony`. For recent versions of V8,
78 `Proxy` must be explicitly enabled with `--harmony_proxies`.
79
80Dependencies
81============
82
83 * ECMAScript 5/strict
84 * To emulate direct proxies:
85 * old Harmony [Proxies](http://wiki.ecmascript.org/doku.php?id=harmony:proxies)
86 * Harmony [WeakMaps](http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps)
87
88After 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:
89
90 Object.getOwnPropertyDescriptor
91 Object.defineProperty
92 Object.defineProperties
93 Object.getOwnPropertyNames
94 Object.getOwnPropertySymbols
95 Object.keys
96 Object.{get,set}PrototypeOf
97 Object.assign
98 Object.{freeze,seal,preventExtensions}
99 Object.{isFrozen,isSealed,isExtensible}
100 Object.prototype.valueOf
101 Object.prototype.isPrototypeOf
102 Object.prototype.toString
103 Object.prototype.hasOwnProperty
104 Function.prototype.toString
105 Date.prototype.toString
106 Array.isArray
107 Array.prototype.concat
108 Proxy
109 Reflect
110
111:warning: In node.js, when you `require('harmony-reflect')`, only the current
112module'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
113back 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
114patched `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.
115
116Examples
117========
118
119The [examples](https://github.com/tvcutsem/harmony-reflect/tree/master/examples) directory contains a number of examples demonstrating the use of proxies:
120
121 * membranes: wrappers that transitively isolate two object-graphs.
122 * observer: a self-hosted implementation of the ES7 `Object.observe` notification mechanism.
123 * profiler: a simple profiler to collect usage statistics of an object.
124
125Other example uses of proxies (not done by me, but using this library):
126
127 * supporting [negative array indices](https://github.com/sindresorhus/negative-array) a la Python
128 * [tpyo](https://github.com/mathiasbynens/tpyo): using proxies to correct typo's in JS property names
129 * [persistent objects](http://tagtree.tv/es6-proxies): shows how one might go about using proxies to save updates to objects in a database incrementally
130 * [defaultdict](https://github.com/greenify/defaultdict2): default values for new keys in objects (as known from Python)
131
132For more examples of proxies, and a good overview of their design rationale, I recommend reading [Axel Rauschmayer's blog post on proxies](http://www.2ality.com/2014/12/es6-proxies.html).
133
134Proxy Handler API
135=================
136
137The sister project [proxy-handlers](https://github.com/tvcutsem/proxy-handlers)
138defines a number of predefined Proxy handlers as "abstract classes" that your
139code can "subclass" The goal is to minimize the number of traps that your proxy
140handlers must implement.
141
142Spec Incompatibilities and other gotcha's
143=========================================
144
145This library differs from the [ECMAScript 2016 spec](http://www.ecma-international.org/ecma-262/7.0/index.html) as follows:
146
147 * In ES7, the `enumerate()` trap, and the corresponding `Reflect.enumerate()` method, have been [removed](https://github.com/tc39/ecma262/issues/161).
148 This shim still supports the trap.
149 * The ES7 (and ES6) spec contains a
150 [bug](https://github.com/tc39/ecma262/pull/666) that leads to missing
151 invariant checks in the getOwnPropertyDescriptor, defineProperty and deleteProperty traps. This library already contains the patch referred
152 to in [this issue](https://github.com/tc39/ecma262/pull/666).
153
154This library differs from the [ECMAScript 2015 spec](http://www.ecma-international.org/ecma-262/6.0/) as follows:
155
156 * In ES6, `Proxy` is a constructor function that _requires_ the use
157 of `new`. That is, you must write `new Proxy(target, handler)`. This library
158 exports `Proxy` as an ordinary function which may be called with or without using the `new` operator.
159
160 * In ES6, `Function.prototype.toString` and `Date.prototype.toString` do not
161 operate transparently on Proxies. This shim patches those functions so that
162 stringifying a Proxy-for-a-function or a Proxy-for-a-date "unwraps" the
163 proxy and instead stringifies the target of the Proxy. This behavior may
164 change in the future to be more spec-compatible.
165
166 * This library does not shim [Symbol objects](http://www.ecma-international.org/ecma-262/6.0/#sec-symbol-objects).
167 On modern V8 or io.js which supports Symbol objects natively, due to a bug in V8, Symbols and Proxies
168 don't play well together. [Read more](https://github.com/tvcutsem/harmony-reflect/issues/57).
169
170 * Proxies-for-arrays are serialized as JSON objects rather than as JSON arrays. That is, `JSON.stringify(new Proxy([], {}))` returns "{}" rather than "[]". [Read more]( https://github.com/tvcutsem/harmony-reflect/issues/13#issuecomment-17249465).
Note: See TracBrowser for help on using the repository browser.