Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Created June 17, 2015 11:52
Show Gist options
  • Save afonsomatos/6ecbb15c8ba8c7e10adc to your computer and use it in GitHub Desktop.
Save afonsomatos/6ecbb15c8ba8c7e10adc to your computer and use it in GitHub Desktop.
New primitive Symbol on ES6
// Primitive value
typeof Symbol(); // 'symbol'
// Unique
Symbol() === Symbol(); // false
// Invoked without `new`
new Symbol(); // TypeError: Symbol is not a constructor
// Set description of Symbol
let sy = Symbol('personal primitive');
String(sy); // 'Symbol(personal primitive)'
// Symbols as property keys and method names
let obj = {}, key = Symbol();
obj[key] = 123;
// getOwnPropertyNames ignores Symbols
let obj = {a: 3, [Symbol('hi')]: 'abc'};
Object.getOwnPropertyNames(obj); // ['a']
Object.getOwnPropertySymbols(obj); // [Symbol(hi)]
// Object.keys() only returns
// enumerable property keys that are strings
Object.keys(obj); // ['a']
// Iterability with Symbol.iterator
// An object is iterable if it has a method
// whose key is Symbol.iterator
let obj = {
[Symbol.iterator()] {
let index = 0;
return { next() { return { value: index++; } } };
}
}
for (let x of obj) console.log(x); // 0.. 1.. 2.. 3.. 4.. ..
// Register for a symbol
let sym = Symbol.for('Sunglasses');
// Retrieve the description (the key)
Symbol.keyFor(sym); // 'Sunglasses'
// Objects are unique, and when working with multiple realms
// Same objects might have different identities
//
// iframe1.html -> s1 = Symbol
// iframe2.html -> s2 = Symbol
//
// s1 !== s2 because they point to different identities
//
// So Symbol() or Symbol.iterator for example, won't work
// among multiple realms. To fix this, libraries / modules
// have to registry and retrieve keys using Symbol.for and
// Symbol.keyFor
// -- Wrapper objects for symbols
let wrapper = Object(Symbol('My Symbol'));
typeof wrapper; // 'object'
wrapper instanceof Symbol; // true
// Square bracket operator [] for properties unwraps string
// wrapper objects and symbol wrapper objects
let sym = Symbol('hello');
let obj = { [sym]: 'a', str: 'b' };
let wrappedSym = Object(sym);
let wrappedStr = new String('bye');
obj[wrappedSym]; // 'a'
obj[wrappedStr]; // 'b'
// Can be converted to boolean and string but not to number
Boolean(Symbol()); // true
String(Symbol()); // 'Symbol()'
Number(Symbol()); // TypeError
// Symbols can't concatenate with strings
"Hello " + Symbol('Mom'); // TypeError
`Good-bye ${ Symbol('Dad') }` // TypeError
// -- Public methods
// Lets an object C customize the behavior of x instanceof C
Symbol.hasInstance // (method)
// Lets an object customize how it is converted to a primitive
// value (first step when something is coerced to a primitive)
Symbol.toPrimitive // (method)
// Called by .toString() to compute the default string description
// of an object obj: '[object Symbol.toStringTag]'
Symbol.toStringTag // (string)
// -- Regular expressions methods
// Used by String.prototype.match
Symbol.match
// Used by String.prototype.replace
Symbol.replace
// Used by String.prototype.search
Symbol.search
// Used by String.prototype.split
Symbol.split
// Lets an object hide some properties from the with statement
Symbol.unscopables // (Object)
// Helps with cloning Typed Arrays and instances of RegExp,
// ArrayBuffer and Promise
Symbol.isConcatSpreadable // (boolean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment