web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
9.0 KiB

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