Skip to content

Instantly share code, notes, and snippets.

@alexspark
Created December 15, 2017 07:08
Show Gist options
  • Save alexspark/97030db52aaf1e9993dc58d63744f68d to your computer and use it in GitHub Desktop.
Save alexspark/97030db52aaf1e9993dc58d63744f68d to your computer and use it in GitHub Desktop.
LearnES6part3 created by alexspark - https://repl.it/@alexspark/LearnES6part3
// Set data structure
let s = new Set();
s.add(1);
s.add(2);
s.add(3);
console.log(s.size);
console.log(s.has(2), s.has(10));
for (let k of s.values()) {
console.log(k);
}
// Map data structure
let m = new Map();
m.set('hello', 42);
m.set('alex', 29);
m.set('eunice', 30);
console.log(m);
for (let [k, v] of m.entries()) {
console.log(`key: ${k}, value: ${v}`)
}
let isMarked = new WeakSet()
let attachedData = new WeakMap()
// weakSet & weakMap
class Node {
constructor (id) { this.id = id }
mark () { isMarked.add(this) }
unmark () { isMarked.delete(this) }
marked () { return isMarked.has(this) }
set data (data) { attachedData.set(this, data) }
get data () { return attachedData.get(this) }
}
let foo = new Node("foo")
JSON.stringify(foo) === '{"id":"foo"}'
foo.mark()
foo.data = "bar"
foo.data === "bar"
JSON.stringify(foo) === '{"id":"foo"}'
isMarked.has(foo) === true
attachedData.has(foo) === true
foo = null /* remove only reference to foo */
attachedData.has(foo) === false
isMarked.has(foo) === false
// type arrays
class Example {
constructor (buffer = new ArrayBuffer(24)) {
this.buffer = buffer
}
set buffer (buffer) {
this._buffer = buffer
this._id = new Uint32Array (this._buffer, 0, 1)
this._username = new Uint8Array (this._buffer, 4, 16)
this._amountDue = new Float32Array(this._buffer, 20, 1)
}
get buffer () { return this._buffer }
set id (v) { this._id[0] = v }
get id () { return this._id[0] }
set username (v) { this._username[0] = v }
get username () { return this._username[0] }
set amountDue (v) { this._amountDue[0] = v }
get amountDue () { return this._amountDue[0] }
}
let example = new Example()
example.id = 7
example.username = "John Doe"
example.amountDue = 42.0
// New built-in methods
Object.assign({}, { a: 'b' }, { a: 'c' });
[1, 2, 3].find(x => x >= 3);
[1, 2, 3].findIndex(x => x >= 3)
'*'.repeat(16);
'hello_world'.startsWith('ello', 1);
'hello_world'.endsWith('world', 11);
Number.isNaN(24);
Number.isFinite(123);
Number.isFinite(NaN);
Number.isFinite(Infinity);
Number.isSafeInteger(42) === true
Number.isSafeInteger(9007199254740992) === false
Number.EPSILON;
console.log(`
all numbers are double-precision 64-bit IEEE 754 values
.2 + .1 = ${0.2 + 0.1}
`);
console.log(0.1 + 0.2 === 0.3) // false
console.log(Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON);
Math.trunc(42.7);
Math.trunc(0.1);
Math.sign(7);
Math.sign(-2);
Math.sign(0);
Math.sign(-0);
Math.sign(NaN);
// Promises
const promFactory = {
*[Symbol.iterator]() {
let count = 0;
for(;;) {
count += 1;
yield new Promise((resolve, reject) => {
if (count < 100) {
resolve(count);
} else {
reject(`count is ${count}. count exceeded 100!`);
}
});
}
}
}
// for (p of promFactory) {
// let ex = false;
// p.then((v) => {
// console.log(`value: ${v}`);
// }).catch(err => {
// console.log(`error: ${err}`);
// ex = true;
// });
// if (ex) break;
// }
// proxying
let target = {
foo: "Welcome, foo"
}
let proxy = new Proxy(target, {
get (receiver, name) {
return name in receiver ? receiver[name] : `Hello, ${name}`
}
})
proxy.foo === "Welcome, foo"
proxy.world === "Hello, world"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment