Published
Edited
Jan 23, 2020
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
{
const foo1 = Symbol('foo');
const foo2 = Symbol('foo');

assert(foo1 !== foo2);
assert(foo1.description === foo2.description);
}
Insert cell
Symbol('A handy description')
Insert cell
Insert cell
new Symbol()
Insert cell
Insert cell
Symbol() instanceof Symbol
Insert cell
typeof Symbol()
Insert cell
typeof String()
Insert cell
typeof new String()
Insert cell
Insert cell
{
const foo1 = Symbol.for('foo');
const foo2 = Symbol.for('foo');

assert(foo1 === foo2);
}
Insert cell
Insert cell
{
const iframe = document.createElement('iframe');

document.body.appendChild(iframe);

assert.equal(iframe.contentWindow.Symbol.for('foo'), Symbol.for('foo'));
assert.notEqual(iframe.contentWindow.Symbol, Symbol);
assert.notEqual(iframe.contentWindow.Symbol.for('foo'), Symbol('foo'));
}
Insert cell
Insert cell
{
const foo1 = Symbol('foo');
const foo2 = Symbol.for('foo');

assert(Symbol.keyFor(foo1) === undefined);
assert(Symbol.keyFor(foo2) === 'foo');
}
Insert cell
Insert cell
Insert cell
{
const foo = Symbol();
'' + foo;
}
Insert cell
{
const foo = Symbol();
+foo;
}
Insert cell
Insert cell
{
const obj = {};
const foo = Symbol();

obj[foo] = 'bar';

assert.deepEqual(Object.getOwnPropertyNames(obj), []);
assert.deepEqual(Object.entries(obj), []);
}
Insert cell
Insert cell
{
const obj = {};
const foo = Symbol();

obj[foo] = 'bar';

assert.deepEqual(Object.getOwnPropertySymbols(obj), [foo]);
assert(obj[foo] === 'bar');
}
Insert cell
Insert cell
{
class MyIterableClass {
*[Symbol.iterator]() {
yield 'I';
yield 'like';
yield 'pizza';
}
}

const instance = new MyIterableClass();
const results = [];

for (const result of instance) {
results.push(result);
}

return results;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more