Slide 1

Slide 1 text

Text Dave Herman September 18, 2013 SYMBOLS: OBJECT OR PRIMITIVE?

Slide 2

Slide 2 text

DESIDERATA

Slide 3

Slide 3 text

1. STATELESSNESS • Sharing a symbol should not share state. • Encapsulates a property key and nothing else.

Slide 4

Slide 4 text

2. CROSS-FRAME COMPAT obj[iterator] = function*() { ... }; let w = new Window(...); w.shared = obj;

Slide 5

Slide 5 text

alert.call() Math.sin(0) document.getElementById("body") (1.2).toFixed() "tc39".toUpperCase() true.toString() 3. METHODS

Slide 6

Slide 6 text

4. MUTABLE PROTOTYPES Yes, monkey-patching in general is bad. But monkey-patching standard methods is a best practice. The evolution of the Web platform depends on it.

Slide 7

Slide 7 text

NON-ANSWERS

Slide 8

Slide 8 text

SHALLOW-FROZEN OBJECTS O.gPO(iterator).foo = 12; Fails Desideratum #1: stateful Fails Desideratum #2: distinct xframe iterators

Slide 9

Slide 9 text

DEEP-FROZEN OBJECTS O.gPO(iterator).foo = 12 // strict error Fails Desideratum #4: no evolution

Slide 10

Slide 10 text

O.gPO(iterator) === null Fails Desideratum #3: no methods PROTOTYPE-FREE OBJECTS

Slide 11

Slide 11 text

NON-WRAPPING PRIMITIVES iterator.valueOf() // error Fails Desideratum #3: no methods

Slide 12

Slide 12 text

CONCLUSION:

Slide 13

Slide 13 text

•JS already has an answer for this! •typeof iterator === "symbol" •Get/call operations auto-wrap •Prototype state is global per-frame •Sending across frames doesn’t share state

Slide 14

Slide 14 text

• I know people think auto-wrapping is gross. • Here’s my positive spin: • Provides a uniform OO surface for all values. • Does so without ruining value immutability. • Does so without ruining API patchability. • Going forward: we need a solution for value types. YES, I DO SEE THAT ELEPHANT

Slide 15

Slide 15 text

REMAINING ISSUES

Slide 16

Slide 16 text

FOOTGUNS? [[ToPropertyKey]] of Symbol objects: auto- unwrap? Does it really matter in practice? Worry about toString for symbols and Symbol objects? Again, does it matter in practice?

Slide 17

Slide 17 text

EXTENDING TYPEOF Do we know it won’t break the Web? MSIE "unknown" type may simply be rare enough to be undiscovered. Fallback: "object" with [[Get]] et al that behave like auto-wrappers? (plus Object.isValue()?)

Slide 18

Slide 18 text

18 SEPT 13 TC39 RESOLUTIONS • Yes to primitives with auto-wrapping • No auto-unwrapping of Symbol objects •typeof iterator === "symbol" • Symbol.prototype.toString should throw to help catch bugs in code evolution; Object.prototype.toString usable for infallible string coercion • Symbol() creates primitive, new Symbol throws