├── .gitignore ├── LICENSE.txt ├── README.md ├── out ├── index.html ├── spec.css └── spec.js ├── package.json ├── reference-implementation ├── README.md └── index.js ├── spec.html └── test └── built-ins └── Object └── getOwnPropertyDescriptors └── has-accessors.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | out/ -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Andrea Giammarchi 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | 24 | The views and conclusions contained in the software and documentation are those 25 | of the authors and should not be interpreted as representing official policies, 26 | either expressed or implied, of the FreeBSD Project. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `Object.getOwnPropertyDescriptors` Proposal ([Polyfill](https://www.npmjs.com/package/object.getownpropertydescriptors)) 2 | 3 | 4 | ## Champion 5 | 6 | At stage 0 [Rick Waldron](https://github.com/rwaldron) agreed to champion this proposal. 7 | However the **current** official Champion is **[Jordan Harband](https://github.com/ljharb)**. 8 | 9 | 10 | 11 | ## Status 12 | 13 | This proposal is currently in [stage 4](https://github.com/tc39/proposals/blob/master/finished-proposals.md) of [the TC39 process](https://github.com/tc39/ecma262/). 14 | 15 | This proposal could be identical to a `Reflect.getOwnPropertyDescriptors` one but for consistency with other plural versions it's described as an `Object` public static method. 16 | 17 | ## Motivation 18 | 19 | There is not a single method in ECMAScript capable of simplifying a proper copy between two objects. 20 | In these days more than ever, where functional programming and immutable objects are essential parts of complex applications, every framework or library is implementing its own boilerplate in order to properly copy properties between composed objects or prototypes. 21 | 22 | There is a lot of confusion and most of the time undesired behavior when it comes to fallback to `Object.assign` because it copies in a way that swallows behavior: it directly accesses properties and symbols instead of their descriptors, discarding possible accessors which could result into an hazard when it come to composing more complex objects or classes’ prototypes. 23 | 24 | Retrieving all descriptors, enumerable or not, is also key to implementing composition over `class`es and their prototypes, since by default they have non-enumerable methods and accessors. 25 | 26 | Also decorators could easily grab at once all descriptors from another class or mixin and assign them through `Object.defineProperties`. 27 | Filtering undesired descriptors would be simpler too, as well as less repetitive each time is needed. 28 | 29 | Last, but not least, a shallow copy between two unknown objects would be free of surprises compared to what `Object.assign` would do. 30 | 31 | 32 | ## FAQs 33 | 34 | ### Should there be a `Reflect.getOwnPropertyDescriptors` ? 35 | 36 | Since the main goal of this proposal is to simplify some common boilerplate and be consistent with the fact there is a singular version of the method but not a plural one, it might be further consistent to have the plural version of the current [Reflect.getOwnPropertyDescriptor](http://www.ecma-international.org/ecma-262/6.0/#sec-reflect.getownpropertydescriptor) method too. 37 | 38 | Update: The committee has previously decided that `Reflect` is solely to mirror `Proxy` traps, so this is not an option. 39 | 40 | 41 | ## Proposed Solution 42 | 43 | As plural version of `Object.getOwnPropertyDescriptor`, this proposal is about retrieving in one single operation all possible own descriptors of a generic object. 44 | 45 | A **polyfill** of such proposal would look like the following: 46 | ```js 47 | if (!Object.hasOwnProperty('getOwnPropertyDescriptors')) { 48 | Object.defineProperty( 49 | Object, 50 | 'getOwnPropertyDescriptors', 51 | { 52 | configurable: true, 53 | writable: true, 54 | value: function getOwnPropertyDescriptors(object) { 55 | return Reflect.ownKeys(object).reduce((descriptors, key) => { 56 | return Object.defineProperty( 57 | descriptors, 58 | key, 59 | { 60 | configurable: true, 61 | enumerable: true, 62 | writable: true, 63 | value: Object.getOwnPropertyDescriptor(object, key) 64 | } 65 | ); 66 | }, {}); 67 | } 68 | } 69 | ); 70 | } 71 | ``` 72 | 73 | 74 | ## Illustrative Examples 75 | 76 | The polyfill shows an alternative, ES2015 friendly, way that improves the boilerplate needed for engines compatible with ES5 or partially with ES2015. 77 | 78 | Now that `Object.getOwnPropertyDescriptors` is in place, all it's needed in order to make a real shallow copy or clone operation between two objects, is shown in the following example: 79 | ```js 80 | const shallowClone = (object) => Object.create( 81 | Object.getPrototypeOf(object), 82 | Object.getOwnPropertyDescriptors(object) 83 | ); 84 | 85 | const shallowMerge = (target, source) => Object.defineProperties( 86 | target, 87 | Object.getOwnPropertyDescriptors(source) 88 | ); 89 | ``` 90 | 91 | Possible objects based mixin solutions could also benefit from this proposal: 92 | ```js 93 | let mix = (object) => ({ 94 | with: (...mixins) => mixins.reduce( 95 | (c, mixin) => Object.create( 96 | c, Object.getOwnPropertyDescriptors(mixin) 97 | ), object) 98 | }); 99 | 100 | // multiple mixins example 101 | let a = {a: 'a'}; 102 | let b = {b: 'b'}; 103 | let c = {c: 'c'}; 104 | let d = mix(c).with(a, b); 105 | ``` 106 | 107 | 108 | Let's say you wanted a version of Object.assign that uses `[[DefineOwnProperty]]`/`[[GetOwnProperty]]` instead of `[[Set]]`/`[[Get]]`, to avoid side effects and copy setters/getters, but still use enumerability as the distinguishing factor. 109 | 110 | Before this proposal, such a method would look like: 111 | ```js 112 | function completeAssign(target, ...sources) { 113 | sources.forEach(source => { 114 | // grab keys descriptors 115 | let descriptors = Object.keys(source).reduce((descriptors, key) => { 116 | descriptors[key] = Object.getOwnPropertyDescriptor(source, key); 117 | return descriptors; 118 | }, {}); 119 | // by default, Object.assign copies enumerable Symbols too 120 | // so grab and filter Symbols as well 121 | Object.getOwnPropertySymbols(source).forEach(sym => { 122 | let descriptor = Object.getOwnPropertyDescriptor(source, sym); 123 | if (descriptor.enumerable) { 124 | descriptors[sym] = descriptor; 125 | } 126 | }); 127 | Object.defineProperties(target, descriptors); 128 | }); 129 | return target; 130 | } 131 | ``` 132 | 133 | However, if `Object.getOwnPropertyDescriptors` was available, above boilerplate would look like: 134 | ```js 135 | var completeAssign = (target, ...sources) => 136 | sources.reduce((target, source) => { 137 | let descriptors = Object.getOwnPropertyDescriptors(source); 138 | Reflect.ownKeys(descriptors).forEach(key => { 139 | if (!descriptors[key].enumerable) { 140 | delete descriptors[key]; 141 | } 142 | }); 143 | return Object.defineProperties(target, descriptors); 144 | }, target); 145 | ``` 146 | -------------------------------------------------------------------------------- /out/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Object.getOwnPropertyDescriptors 9 | 20 | 21 | 22 | 23 | 24 | 34 |

Stage 2 Draft / January 28, 2016

35 |

Object.getOwnPropertyDescriptors

36 | 37 | 38 |

1Object.getOwnPropertyDescriptors ( O )#

39 | 40 |

When the getOwnPropertyDescriptors function is called, the following steps are taken:

41 | 42 | 43 |
    44 |
  1. Let obj be ? 45 | ToObject(O).
  2. 46 |
  3. Let ownKeys be ? obj.[[OwnPropertyKeys]]().
  4. 47 |
  5. Let descriptors be ! 48 | ObjectCreate( 49 | %ObjectPrototype%).
  6. 50 |
  7. Repeat, for each element key of ownKeys in 51 | List order, 52 |
      53 |
    1. Let desc be ? obj.[[GetOwnProperty]](key).
    2. 54 |
    3. Let descriptor be ! 55 | FromPropertyDescriptor(desc).
    4. 56 |
    5. Perform ! 57 | CreateDataProperty(descriptors, key, descriptor).
    6. 58 |
    59 |
  8. 60 |
  9. Return descriptors. 61 |
  10. 62 |
63 |
64 | 65 |
66 | -------------------------------------------------------------------------------- /out/spec.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size: 18px; 3 | line-height: 1.5; 4 | font-family: Cambria, Palatino Linotype, Palatino, Liberation Serif, serif; 5 | padding: 0; 6 | color: #333; 7 | margin: 0 2% 0 31%; 8 | } 9 | 10 | body.oldtoc { 11 | margin: 0 auto; 12 | } 13 | 14 | a { 15 | text-decoration: none; 16 | color: #206ca7; 17 | } 18 | 19 | a:visited { 20 | color: #206ca7; 21 | } 22 | 23 | a:hover { 24 | text-decoration: underline; 25 | color: #239dee; 26 | } 27 | 28 | 29 | code { 30 | font-weight: bold; 31 | font-family: Consolas, Monaco, monospace; 32 | white-space: pre; 33 | } 34 | 35 | pre code { 36 | font-weight: inherit; 37 | } 38 | 39 | pre code.hljs { 40 | background-color: #fff; 41 | margin: 0; 42 | padding: 0; 43 | } 44 | 45 | ol.toc { 46 | list-style: none; 47 | padding-left: 0; 48 | } 49 | 50 | ol.toc ol.toc { 51 | padding-left: 2ex; 52 | list-style: none; 53 | } 54 | 55 | var { 56 | color: #2aa198; 57 | transition: background-color 0.25s ease; 58 | cursor: pointer; 59 | } 60 | 61 | var.referenced { 62 | background-color: #ffff33; 63 | } 64 | 65 | emu-const { 66 | font-family: sans-serif; 67 | } 68 | 69 | emu-val { 70 | font-weight: bold; 71 | } 72 | emu-alg ol, emu-alg ol ol ol ol { 73 | list-style-type: decimal; 74 | } 75 | 76 | emu-alg ol ol, emu-alg ol ol ol ol ol { 77 | list-style-type: lower-alpha; 78 | } 79 | 80 | emu-alg ol ol ol, ol ol ol ol ol ol { 81 | list-style-type: lower-roman; 82 | } 83 | 84 | emu-eqn { 85 | display: block; 86 | margin-left: 4em; 87 | } 88 | 89 | emu-eqn div:first-child { 90 | margin-left: -2em; 91 | } 92 | 93 | emu-eqn.inline { 94 | display: inline; 95 | margin: 0; 96 | white-space: nowrap; 97 | } 98 | 99 | emu-note { 100 | display: block; 101 | margin: 1em 0 1em 6em; 102 | color: #666; 103 | } 104 | 105 | emu-note span.note { 106 | text-transform: uppercase; 107 | margin-left: -6em; 108 | display: block; 109 | float: left; 110 | } 111 | 112 | emu-example { 113 | display: block; 114 | margin: 1em 3em; 115 | } 116 | 117 | emu-example figure figcaption { 118 | margin-top: 0.5em; 119 | text-align: left; 120 | } 121 | 122 | emu-production { 123 | display: block; 124 | margin-top: 1em; 125 | margin-bottom: 1em; 126 | margin-left: 5ex; 127 | } 128 | 129 | 130 | emu-grammar.inline, emu-production.inline, 131 | emu-grammar.inline emu-production emu-rhs, emu-production.inline emu-rhs { 132 | display: inline; 133 | } 134 | 135 | emu-grammar[collapsed] emu-production, emu-production[collapsed] { 136 | margin: 0; 137 | } 138 | 139 | emu-grammar[collapsed] emu-production emu-rhs, emu-production[collapsed] emu-rhs { 140 | display: inline; 141 | padding-left: 1ex; 142 | } 143 | 144 | emu-constraints { 145 | font-size: .75em; 146 | margin-right: 1ex; 147 | } 148 | 149 | emu-gann { 150 | margin-right: 1ex; 151 | } 152 | 153 | emu-gann emu-t:last-child, 154 | emu-gann emu-nt:last-child { 155 | margin-right: 0; 156 | } 157 | 158 | emu-geq { 159 | margin-left: 1ex; 160 | font-weight: bold; 161 | } 162 | 163 | emu-oneof { 164 | font-weight: bold; 165 | margin-left: 1ex; 166 | } 167 | 168 | emu-nt { 169 | display: inline-block; 170 | font-style: italic; 171 | white-space: nowrap; 172 | text-indent: 0; 173 | } 174 | 175 | emu-nt a, emu-nt a:visited { 176 | color: #333; 177 | } 178 | 179 | emu-rhs emu-nt { 180 | margin-right: 1ex; 181 | } 182 | 183 | emu-t { 184 | display: inline-block; 185 | font-family: monospace; 186 | font-weight: bold; 187 | white-space: nowrap; 188 | text-indent: 0; 189 | } 190 | 191 | emu-production emu-t { 192 | margin-right: 1ex; 193 | } 194 | 195 | emu-rhs { 196 | display: block; 197 | padding-left: 75px; 198 | text-indent: -25px; 199 | } 200 | 201 | emu-mods { 202 | font-size: .85em; 203 | vertical-align: sub; 204 | font-style: normal; 205 | font-weight: normal; 206 | } 207 | 208 | emu-production[collapsed] emu-mods { 209 | display: none; 210 | } 211 | 212 | emu-params, emu-opt { 213 | margin-right: 1ex; 214 | font-family: monospace; 215 | } 216 | 217 | emu-params, emu-constraints { 218 | color: #2aa198; 219 | } 220 | 221 | emu-opt { 222 | color: #b58900; 223 | } 224 | 225 | emu-gprose { 226 | font-size: 0.9em; 227 | font-family: Helvetica, Arial, sans-serif; 228 | } 229 | 230 | h1.shortname { 231 | color: #f60; 232 | font-size: 1.5em; 233 | margin: 0; 234 | } 235 | h1.version { 236 | color: #f60; 237 | font-size: 1.5em; 238 | margin: 0; 239 | } 240 | h1.title { 241 | margin-top: 0; 242 | color: #f60; 243 | } 244 | h1, h2, h3, h4, h5, h6 { 245 | position: relative; 246 | } 247 | h1 .secnum { 248 | position: absolute; 249 | text-align: right; 250 | right: 100%; 251 | margin-right: 1ex; 252 | white-space: nowrap; 253 | } 254 | 255 | h1 { font-size: 2.67em; } 256 | h2 { font-size: 2em; } 257 | h3 { font-size: 1.56em; } 258 | h4 { font-size: 1.25em; } 259 | h5 { font-size: 1.11em; } 260 | h6 { font-size: 1em; } 261 | 262 | h1 span.utils, 263 | h2 span.utils, 264 | h3 span.utils, 265 | h4 span.utils, 266 | h5 span.utils, 267 | h6 span.utils { 268 | padding-left: 1em; 269 | } 270 | 271 | h1 span.utils span.anchor a, 272 | h2 span.utils span.anchor a, 273 | h3 span.utils span.anchor a, 274 | h4 span.utils span.anchor a, 275 | h5 span.utils span.anchor a, 276 | h6 span.utils span.anchor a { 277 | color: #ccc; 278 | text-decoration: none; 279 | } 280 | 281 | h1 span.utils span.anchor a:hover, 282 | h2 span.utils span.anchor a:hover, 283 | h3 span.utils span.anchor a:hover, 284 | h4 span.utils span.anchor a:hover, 285 | h5 span.utils span.anchor a:hover, 286 | h6 span.utils span.anchor a:hover { 287 | color: #333; 288 | } 289 | 290 | emu-intro h1, emu-clause h1, emu-annex h1 { font-size: 2em; } 291 | emu-intro h2, emu-clause h2, emu-annex h2 { font-size: 1.56em; } 292 | emu-intro h3, emu-clause h3, emu-annex h3 { font-size: 1.25em; } 293 | emu-intro h4, emu-clause h4, emu-annex h4 { font-size: 1.11em; } 294 | emu-intro h5, emu-clause h5, emu-annex h5 { font-size: 1em; } 295 | emu-intro h6, emu-clause h6, emu-annex h6 { font-size: 0.9em; } 296 | emu-intro emu-intro h1, emu-clause emu-clause h1, emu-annex emu-annex h1 { font-size: 1.56em; } 297 | emu-intro emu-intro h2, emu-clause emu-clause h2, emu-annex emu-annex h2 { font-size: 1.25em; } 298 | emu-intro emu-intro h3, emu-clause emu-clause h3, emu-annex emu-annex h3 { font-size: 1.11em; } 299 | emu-intro emu-intro h4, emu-clause emu-clause h4, emu-annex emu-annex h4 { font-size: 1em; } 300 | emu-intro emu-intro h5, emu-clause emu-clause h5, emu-annex emu-annex h5 { font-size: 0.9em; } 301 | emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex h1 { font-size: 1.25em; } 302 | emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex h2 { font-size: 1.11em; } 303 | emu-intro emu-intro emu-intro h3, emu-clause emu-clause emu-clause h3, emu-annex emu-annex emu-annex h3 { font-size: 1em; } 304 | emu-intro emu-intro emu-intro h4, emu-clause emu-clause emu-clause h4, emu-annex emu-annex emu-annex h4 { font-size: 0.9em; } 305 | emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex h1 { font-size: 1.11em; } 306 | emu-intro emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex emu-annex h2 { font-size: 1em; } 307 | emu-intro emu-intro emu-intro emu-intro h3, emu-clause emu-clause emu-clause emu-clause h3, emu-annex emu-annex emu-annex emu-annex h3 { font-size: 0.9em; } 308 | emu-intro emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex emu-annex h1 { font-size: 1em; } 309 | emu-intro emu-intro emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex emu-annex emu-annex h2 { font-size: 0.9em; } 310 | emu-intro emu-intro emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex emu-annex emu-annex h1 { font-size: 0.9em } 311 | 312 | emu-clause { 313 | display: block; 314 | } 315 | 316 | /* Figures and tables */ 317 | figure { display: block; margin: 1em 0 3em 0; } 318 | figure object { display: block; margin: 0 auto; } 319 | figure table.real-table { margin: 0 auto; } 320 | figure figcaption { 321 | display: block; 322 | color: #555555; 323 | font-weight: bold; 324 | text-align: center; 325 | } 326 | 327 | emu-table table { 328 | margin: 0 auto; 329 | } 330 | 331 | emu-table table, table.real-table { 332 | border-collapse: collapse; 333 | } 334 | 335 | emu-table td, emu-table th, table.real-table td, table.real-table th { 336 | border: 1px solid black; 337 | padding: 0.4em; 338 | vertical-align: baseline; 339 | } 340 | emu-table th, emu-table thead td, table.real-table th { 341 | background-color: #eeeeee; 342 | } 343 | 344 | /* Note: the left content edges of table.lightweight-table >tbody >tr >td 345 | and div.display line up. */ 346 | table.lightweight-table { 347 | border-collapse: collapse; 348 | margin: 0 0 0 1.5em; 349 | } 350 | table.lightweight-table td, table.lightweight-table th { 351 | border: none; 352 | padding: 0 0.5em; 353 | vertical-align: baseline; 354 | } 355 | 356 | /* diff styles */ 357 | ins { 358 | background-color: #e0f8e0; 359 | text-decoration: none; 360 | border-bottom: 1px solid #396; 361 | } 362 | 363 | ins.block { 364 | display: block; 365 | } 366 | 367 | del { 368 | background-color: #fee; 369 | } 370 | 371 | del.block { 372 | display: block; 373 | } 374 | 375 | /* Menu Styles */ 376 | #menu-toggle { 377 | font-size: 2em; 378 | 379 | position: fixed; 380 | top: 0; 381 | left: 0; 382 | width: 1.5em; 383 | height: 1.5em; 384 | z-index: 3; 385 | visibility: hidden; 386 | 387 | background-color: #111; 388 | color: #B6C8E4; 389 | 390 | line-height: 1.5em; 391 | text-align: center; 392 | -webkit-touch-callout: none; 393 | -webkit-user-select: none; 394 | -khtml-user-select: none; 395 | -moz-user-select: none; 396 | -ms-user-select: none; 397 | user-select: none;; 398 | 399 | cursor: pointer; 400 | } 401 | 402 | #menu { 403 | position: fixed; 404 | left: 0; 405 | top: 0; 406 | height: 100%; 407 | width: 24%; 408 | z-index: 2; 409 | overflow-x: hidden; 410 | overflow-y: auto; 411 | box-sizing: border-box; 412 | 413 | background-color: #111; 414 | 415 | transition: opacity 0.1s linear; 416 | } 417 | 418 | #menu.active { 419 | display: block; 420 | opacity: 1; 421 | } 422 | 423 | #menu-toc > ol { 424 | padding: 0; 425 | } 426 | 427 | #menu-toc > ol , #menu-toc > ol ol { 428 | list-style-type: none; 429 | } 430 | 431 | #menu-toc > ol ol { 432 | padding-left: 0.75em; 433 | } 434 | 435 | #menu-toc li { 436 | text-overflow: ellipsis; 437 | overflow: hidden; 438 | white-space: nowrap; 439 | } 440 | 441 | #menu-toc .item-toggle { 442 | display: inline-block; 443 | transform: rotate(-45deg) translate(-5px, -5px); 444 | transition: transform 0.1s ease; 445 | width: 1em; 446 | 447 | color: #555F6E; 448 | 449 | -webkit-touch-callout: none; 450 | -webkit-user-select: none; 451 | -khtml-user-select: none; 452 | -moz-user-select: none; 453 | -ms-user-select: none; 454 | user-select: none;; 455 | 456 | cursor: pointer; 457 | } 458 | 459 | #menu-toc .item-toggle-none { 460 | display: inline-block; 461 | width: 1em; 462 | } 463 | 464 | #menu-toc li.active > .item-toggle { 465 | transform: rotate(45deg) translate(-5px, -5px); 466 | } 467 | 468 | #menu-toc li > ol { 469 | display: none; 470 | } 471 | 472 | #menu-toc li.active > ol { 473 | display: block; 474 | } 475 | 476 | #menu-toc li > a { 477 | padding-left: 0.25em; 478 | color: #B6C8E4; 479 | } 480 | 481 | #menu-search { 482 | color: #B6C8E4; 483 | } 484 | 485 | #menu-search-box { 486 | display: block; 487 | width: 90%; 488 | margin: 5px auto; 489 | font-size: 1em; 490 | padding: 2px; 491 | } 492 | 493 | #menu-search-results.inactive { 494 | display: none; 495 | } 496 | 497 | #menu-search-results ul { 498 | list-style-type: square; 499 | padding: 0 0 0 35px; 500 | margin: 0; 501 | } 502 | 503 | #menu-search-results li { 504 | white-space: nowrap; 505 | } 506 | 507 | #menu-search-results a { 508 | color: #b6c8e4; 509 | } 510 | 511 | @media (max-width: 1366px) { 512 | body { 513 | margin: 0 0 0 150px; 514 | } 515 | 516 | #menu { 517 | display: none; 518 | padding-top: 3em; 519 | width: 323px; 520 | } 521 | 522 | #menu-toggle { 523 | visibility: visible; 524 | } 525 | } 526 | 527 | @media only screen and (max-width: 800px) { 528 | body { 529 | margin: 2em 10px 0 10px; 530 | } 531 | 532 | #menu { 533 | width: 100%; 534 | } 535 | 536 | h1 .secnum { 537 | display: inline; 538 | position: inherit; 539 | left: 0; 540 | right: 0; 541 | } 542 | 543 | h1 .secnum:empty { 544 | margin: 0; padding: 0; 545 | } 546 | } 547 | -------------------------------------------------------------------------------- /out/spec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function Menu() { 4 | this.$toggle = document.getElementById('menu-toggle'); 5 | this.$menu = document.getElementById('menu'); 6 | this.$searchBox = document.getElementById('menu-search-box'); 7 | this.$searchResults = document.getElementById('menu-search-results'); 8 | this.initSearch(); 9 | 10 | this.$toggle.addEventListener('click', this.toggle.bind(this)); 11 | 12 | this.$searchBox.addEventListener('keydown', function (e) { 13 | if (e.keyCode === 191 && e.target.value.length === 0) { 14 | e.preventDefault(); 15 | e.stopPropagation(); 16 | } else if (e.keyCode === 13) { 17 | e.preventDefault(); 18 | e.stopPropagation(); 19 | this.selectResult(); 20 | } 21 | }.bind(this)); 22 | 23 | this.$searchBox.addEventListener('keyup', debounce(function (e) { 24 | e.stopPropagation(); 25 | this.search(e.target.value); 26 | }.bind(this))); 27 | 28 | 29 | var tocItems = this.$menu.querySelectorAll('#menu-toc li'); 30 | for (var i = 0; i < tocItems.length; i++) { 31 | var $item = tocItems[i]; 32 | $item.addEventListener('click', function($item, event) { 33 | $item.classList.toggle('active'); 34 | event.stopPropagation(); 35 | }.bind(null, $item)); 36 | } 37 | 38 | var tocLinks = this.$menu.querySelectorAll('#menu-toc li > a'); 39 | for (var i = 0; i < tocLinks.length; i++) { 40 | var $link = tocLinks[i]; 41 | $link.addEventListener('click', function(event) { 42 | this.toggle(); 43 | event.stopPropagation(); 44 | }.bind(this)); 45 | } 46 | } 47 | 48 | Menu.prototype.toggle = function () { 49 | this.$menu.classList.toggle('active'); 50 | } 51 | 52 | Menu.prototype.show = function () { 53 | this.$menu.classList.add('active'); 54 | } 55 | 56 | Menu.prototype.hide = function () { 57 | this.$menu.classList.remove('active'); 58 | } 59 | 60 | Menu.prototype.isVisible = function() { 61 | return this.$menu.classList.contains('active'); 62 | } 63 | 64 | Menu.prototype.initSearch = function () { 65 | var $biblio = document.getElementById('menu-search-biblio'); 66 | if (!$biblio) { 67 | this.biblio = {}; 68 | } else { 69 | this.biblio = JSON.parse($biblio.textContent); 70 | } 71 | 72 | this.biblio.ops = this.biblio.filter(function (e) { return e.type === 'op' }); 73 | this.biblio.clauses = this.biblio.filter(function (e) { return e.type === 'clause' }); 74 | this.biblio.productions = this.biblio.filter(function (e) { return e.type === 'production' }); 75 | 76 | document.addEventListener('keydown', function (e) { 77 | if (e.keyCode === 191) { 78 | e.preventDefault(); 79 | e.stopPropagation(); 80 | 81 | if(this.isVisible()) { 82 | this._closeAfterSearch = false; 83 | } else { 84 | this._closeAfterSearch = true; 85 | this.show(); 86 | } 87 | 88 | this.show(); 89 | this.$searchBox.focus(); 90 | } 91 | }.bind(this)) 92 | } 93 | 94 | Menu.prototype.search = function (needle) { 95 | if (needle.length < 2) { 96 | this.hideSearch(); 97 | } else { 98 | this.showSearch(); 99 | } 100 | 101 | needle = needle.toLowerCase(); 102 | 103 | var results = {}; 104 | var seenClauses = {}; 105 | 106 | results.ops = this.biblio.ops.filter(function(op) { 107 | return fuzzysearch(needle, op.aoid.toLowerCase()); 108 | }); 109 | 110 | results.ops.forEach(function(op) { 111 | seenClauses[op.refId] = true; 112 | }); 113 | 114 | results.productions = this.biblio.productions.filter(function(prod) { 115 | return fuzzysearch(needle, prod.name.toLowerCase()); 116 | }); 117 | 118 | results.clauses = this.biblio.clauses.filter(function(clause) { 119 | return !seenClauses[clause.id] && (clause.number.indexOf(needle) === 0 || fuzzysearch(needle, clause.title.toLowerCase())); 120 | }); 121 | 122 | if (results.length > 50) { 123 | results = results.slice(0, 50); 124 | } 125 | 126 | this.displayResults(results); 127 | } 128 | 129 | Menu.prototype.displayResults = function (results) { 130 | var totalResults = Object.keys(results).reduce(function (sum, record) { return sum + record.length }, 0); 131 | 132 | if (totalResults > 0) { 133 | this.$searchResults.classList.remove('no-results'); 134 | 135 | var html = '' 150 | 151 | this.$searchResults.innerHTML = html; 152 | } else { 153 | this.$searchResults.classList.add('no-results'); 154 | } 155 | } 156 | 157 | Menu.prototype.hideSearch = function () { 158 | this.$searchResults.classList.add('inactive'); 159 | } 160 | 161 | Menu.prototype.showSearch = function () { 162 | this.$searchResults.classList.remove('inactive'); 163 | } 164 | 165 | Menu.prototype.selectResult = function () { 166 | var $first = this.$searchResults.querySelector('li:first-child a'); 167 | 168 | if ($first) { 169 | document.location = $first.getAttribute('href'); 170 | } 171 | 172 | this.$searchBox.value = ''; 173 | this.$searchBox.blur(); 174 | this.hideSearch(); 175 | 176 | if (this._closeAfterSearch) { 177 | this.hide(); 178 | } 179 | } 180 | 181 | function init() { 182 | var menu = new Menu(); 183 | } 184 | 185 | document.addEventListener('DOMContentLoaded', init); 186 | 187 | function debounce(fn) { 188 | var timeout; 189 | return function() { 190 | var args = arguments; 191 | if (timeout) { 192 | clearTimeout(timeout); 193 | } 194 | timeout = setTimeout(function() { 195 | timeout = null; 196 | fn.apply(this, args); 197 | }.bind(this), 150); 198 | } 199 | } 200 | 201 | // The following license applies to the fuzzysearch function 202 | // The MIT License (MIT) 203 | // Copyright © 2015 Nicolas Bevacqua 204 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 205 | // this software and associated documentation files (the "Software"), to deal in 206 | // the Software without restriction, including without limitation the rights to 207 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 208 | // the Software, and to permit persons to whom the Software is furnished to do so, 209 | // subject to the following conditions: 210 | 211 | // The above copyright notice and this permission notice shall be included in all 212 | // copies or substantial portions of the Software. 213 | 214 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 215 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 216 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 217 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 218 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 219 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 220 | function fuzzysearch (needle, haystack) { 221 | var tlen = haystack.length; 222 | var qlen = needle.length; 223 | if (qlen > tlen) { 224 | return false; 225 | } 226 | if (qlen === tlen) { 227 | return needle === haystack; 228 | } 229 | outer: for (var i = 0, j = 0; i < qlen; i++) { 230 | var nch = needle.charCodeAt(i); 231 | while (j < tlen) { 232 | if (haystack.charCodeAt(j++) === nch) { 233 | continue outer; 234 | } 235 | } 236 | return false; 237 | } 238 | return true; 239 | } 240 | var CLAUSE_NODES = ['EMU-CLAUSE', 'EMU-INTRO', 'EMU-ANNEX']; 241 | function findLocalReferences ($elem) { 242 | var name = $elem.innerHTML; 243 | var references = []; 244 | 245 | var parentClause = $elem.parentNode; 246 | while (parentClause && CLAUSE_NODES.indexOf(parentClause.nodeName) === -1) { 247 | parentClause = parentClause.parentNode; 248 | } 249 | 250 | if(!parentClause) return; 251 | 252 | var vars = parentClause.querySelectorAll('var'); 253 | 254 | for (var i = 0; i < vars.length; i++) { 255 | var $var = vars[i]; 256 | 257 | if ($var.innerHTML === name) { 258 | references.push($var); 259 | } 260 | } 261 | 262 | return references; 263 | } 264 | 265 | function toggleFindLocalReferences($elem) { 266 | var references = findLocalReferences($elem); 267 | if ($elem.classList.contains('referenced')) { 268 | references.forEach(function ($reference) { 269 | $reference.classList.remove('referenced'); 270 | }); 271 | } else { 272 | references.forEach(function ($reference) { 273 | $reference.classList.add('referenced'); 274 | }); 275 | } 276 | } 277 | 278 | function installFindLocalReferences () { 279 | document.addEventListener('click', function (e) { 280 | if (e.target.nodeName === 'VAR') { 281 | toggleFindLocalReferences(e.target); 282 | } 283 | }); 284 | } 285 | 286 | document.addEventListener('DOMContentLoaded', installFindLocalReferences); 287 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ecma-proposal-object.getownpropertydescriptors", 3 | "version": "0.0.0", 4 | "description": "Tests and a polyfill for the ES proposal for Object.getOwnPropertyDescriptors", 5 | "main": "reference-implementation/index.js", 6 | "scripts": { 7 | "clean": "rm -rf out", 8 | "build": "npm run clean && mkdir -p out && ecmarkup spec.html --js=out/spec.js --css=out/spec.css | js-beautify -f - --type=html -t > out/index.html", 9 | "test": "test262-harness --prelude=reference-implementation/index.js --consoleCommand=\"node --harmony\" --runner=console test/built-ins/Object/getOwnPropertyDescriptors/*.js" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/tc39/proposal-object-getownpropertydescriptors.git" 14 | }, 15 | "keywords": [ 16 | "ecmascript", 17 | "object", 18 | "test262", 19 | "getownpropertydescriptors", 20 | "Object.getOwnPropertyDescriptor", 21 | "Object.getOwnPropertyDescriptors" 22 | ], 23 | "author": "Andrea Giammarchi", 24 | "license": "MIT", 25 | "devDependencies": { 26 | "ecmarkup": "^3.0.1", 27 | "js-beautify": "^1.6.2", 28 | "test262-harness": "^1.5.6" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /reference-implementation/README.md: -------------------------------------------------------------------------------- 1 | # `Object.getOwnPropertyDescriptors` Reference Implementation 2 | 3 | The reference implementation is meant to be a line-by-line transcription of the specification from ECMASpeak into JavaScript, as much as is possible. 4 | 5 | Its purpose is to provide a 100%-fidelity implementation to run tests against in order to check the spec logic. In particular, it is not intended be a usable implementation or polyfill. -------------------------------------------------------------------------------- /reference-implementation/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | if (typeof Reflect === 'undefined') { 4 | global.Reflect = { 5 | defineProperty: Object.defineProperty, 6 | getOwnPropertyDescriptor: Object.getOwnPropertyDescriptor, 7 | ownKeys: function ownKeys(genericObject) { 8 | let gOPS = Object.getOwnPropertySymbols || function () { return []; }; 9 | return Object.getOwnPropertyNames(genericObject) 10 | .concat(gOPS(genericObject)); 11 | } 12 | }; 13 | } 14 | 15 | Object.defineProperty( 16 | Object, 17 | 'getOwnPropertyDescriptors', 18 | { 19 | configurable: true, 20 | writable: true, 21 | value: function getOwnPropertyDescriptors(genericObject) { 22 | // Let `obj` be ? `ToObject(O)` 23 | if (Object(genericObject) !== genericObject) { 24 | throw new Error('Argument should be an object'); 25 | } 26 | 27 | // Let `ownKeys` be the result of calling ? `obj.[[OwnPropertyKeys]]()` 28 | let ownKeys; 29 | try { 30 | ownKeys = Reflect.ownKeys(genericObject); 31 | } catch(e) { 32 | throw new Error('Unable to retrieve own keys'); 33 | } 34 | 35 | // Let `descriptors` be ? `ObjectCreate(%ObjectPrototype%)` 36 | let descriptors; 37 | try { 38 | descriptors = Object.create(Object.prototype); 39 | } catch(e) { 40 | throw new Error('Unable to create an instance of Object.prototype'); 41 | } 42 | 43 | // Repeat, for each element `key` of `ownKeys` in List order 44 | for (let key of ownKeys) { 45 | 46 | // Let `desc` be the result of ? `obj.[[GetOwnProperty]](key)` 47 | // Let `descriptor` be ? `FromPropertyDescriptor(desc)` 48 | let descriptor = Reflect.getOwnPropertyDescriptor(genericObject, key); 49 | 50 | if (typeof descriptor !== 'undefined') { 51 | // Let `status` be the result of ? `CreateDataProperty(descriptors, key, descriptor)` 52 | try { 53 | Reflect.defineProperty(descriptors, key, { 54 | configurable: true, 55 | enumerable: true, 56 | writable: true, 57 | value: descriptor 58 | }); 59 | } catch(e) { 60 | throw new Error('Unable to create a data propoerty'); 61 | } 62 | } 63 | } 64 | 65 | // Return `descriptors` 66 | return descriptors; 67 | } 68 | } 69 | ); -------------------------------------------------------------------------------- /spec.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
 7 | title: Object.getOwnPropertyDescriptors
 8 | stage: 2
 9 | location: https://github.com/tc39/proposal-object-getownpropertydescriptors
10 | copyright: false
11 | contributors: Andrea Giammarchi, Jordan Harband
12 | 
13 | 14 | 15 |

Object.getOwnPropertyDescriptors ( _O_ )

16 | 17 |

When the `getOwnPropertyDescriptors` function is called, the following steps are taken:

18 | 19 | 20 | 1. Let _obj_ be ? ToObject(_O_). 21 | 1. Let _ownKeys_ be ? _obj_.[[OwnPropertyKeys]](). 22 | 1. Let _descriptors_ be ! ObjectCreate(%ObjectPrototype%). 23 | 1. Repeat, for each element _key_ of _ownKeys_ in List order, 24 | 1. Let _desc_ be ? _obj_.[[GetOwnProperty]](_key_). 25 | 1. Let _descriptor_ be ! FromPropertyDescriptor(_desc_). 26 | 1. Perform ! CreateDataProperty(_descriptors_, _key_, _descriptor_). 27 | 1. Return _descriptors_. 28 | 29 | 30 |
-------------------------------------------------------------------------------- /test/built-ins/Object/getOwnPropertyDescriptors/has-accessors.js: -------------------------------------------------------------------------------- 1 | var a = {get a() {}}; 2 | var b = Object.getOwnPropertyDescriptors(a); 3 | 4 | 5 | assert(b.a.get === Object.getOwnPropertyDescriptor(a, 'a').get, 6 | 'Expected descriptors.a.get to be exact same of Object.getOwnPropertyDescriptor(object, "a").get'); --------------------------------------------------------------------------------