import e from 'examples.js';
// e.defaultFunctionParameters();
// e.restParameters();
// e.spreadOperator();
// e.objectLiteralExtensions();
// e.destructuring();
// e.classes();
// e.bindings();
// e.templates();
// e.arrowFunctions();
// e.symbols();
// e.mapsAndSets();
// e.forOfLoops();
// e.generators();
// e.proxiesAndReflections();
// e.miscellaneous();
export default {
defaultFunctionParameters() {
const basicStuff = (a=1, b=2) => console.log(a, b);
basicStuff(3, 4); // 3 4
basicStuff(3); // 3 2
basicStuff(); // 1 2
basicStuff(undefined, 4); // 1 4
const previousParams = (a=1, b=a) => console.log(a, b);
previousParams(3, 4); // 3 4
previousParams(3); // 3 3
previousParams(); // 1 1
previousParams(undefined, 4); // 1 4
},
restParameters() {
const basicStuff = (a, ...b) => console.log(a, b);
basicStuff(1, 2, 3); // 1, [2, 3]
basicStuff(1); // 1, []
/*
* The following does NOT work
* The rest parameter must be the last one
*/
// const restParamIsLastParam = (...a, b) => {};
},
spreadOperator() {
console.log(...['Hello', 'world!']); // Hello world!
console.log('Hello', ...['world!']); // Hello world!
console.log('Hello', ...[], 'world!'); // Hello world!
console.log('Hello', ...[], 'world!', ...[]); // Hello world!
// Bonus, transpose a matrix with underscore and the spread operator
// const myMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
//_.zip(...myMatrix);
},
objectLiteralExtensions() {
const hello = 'Hello';
console.log({ [hello]: 'world!' }); // { Hello: 'world!' }
const Hello = 'world!';
console.log({ Hello }); // { Hello: 'world!' }
const myHelloObject = {
hello() { console.log('Hello world!'); },
'Hello world!'() { console.log('Hello world!'); },
[hello]() { console.log('Hello world!'); }
};
myHelloObject.hello(); // Hello world!
myHelloObject['Hello world!'](); // Hello world!
myHelloObject.Hello(); // Hello world!
},
classes() {
class Hello {
constructor() {console.log('Hello');}
}
class HelloWorld extends Hello {
constructor() {super(); console.log('world!');}
}
new Hello(); // Hello
new HelloWorld(); // Hello\nworld!
},
destructuring() {
let [a, b] = [1, 2];
console.log(a, b); // 1 2
let [c, d] = [1];
console.log(c, d); // 1 undefined
let [e, , f, [g], ...h] = [1, 2, , [3, 4], 6, 7];
console.log(e, f, g, h); // 1 undefined 3 [6, 7]
let [i, j,] = 'Hello world!';
console.log(i, j); // H e
// Works with any iterable
const world = 'world';
let { k, hello: l, m, [world]: n, oneliner: { o, p=1} } = {
k: 'Hello',
hello: 'world!',
world: '!',
oneliner: { o: 2 }
};
console.log(k, l, m, n, o, p); // Hello world! undefined ! 2 1
// And all of this works in function parameters as well
},
bindings() {
let a = 1;
const B = 2;
for (let i=0; i<1; i++) {
let j = i;
function hello() { console.log('Hello world!'); }
}
// i, j and hello are not defined here
// What it implies
for (var k=0; k<2; k++) {
setTimeout(() => console.log(k), 0); // 2 2
}
for (let l=0; l<2; l++) {
setTimeout(() => console.log(l), 0); // 0 1
}
},
templates() {
console.log(`
Hello!`); // '\n Hello!'
const hello = 'Hello';
console.log(`${hello}`); // Hello
console.log(`${hello.toLowerCase() + ' world!'}`); // hello world!
function lower(parts, ...params) {
let i, result = '';
for (i=0; i<params.length; i++) {
result += parts[i] + params[i].toLowerCase();
}
result += parts[i];
return result;
}
console.log(lower`A${'B'}C${'D'}E`); // AbCdE
},
arrowFunctions() {
() => {};
a => 1;
(a, b) => 1;
const testContext = () => console.log(this);
testContext(); // Our current object
testContext.call({}); // Still our current object
testContext.bind({})(); // Again still our current object
const canBeCurriedThough = (a, b) => console.log(a, b);
canBeCurriedThough.bind(null, 1)(2); // 1 2
},
symbols() {
const mySymbol = Symbol('optionalDescription');
const myObject = {};
myObject[mySymbol] = 'Hello world!';
console.log(myObject); // {}
console.log(myObject[mySymbol]); // Hello world!
const theodoSymbol = Symbol.for('Theodo');
console.log(theodoSymbol === Symbol.for('Theodo')); // true
console.log(Symbol.keyFor(theodoSymbol)); // Theodo
// Symbol registry in Symbol: e.g. Symbol.iterator
// Example
const myXML = {
[Symbol.for('type')]: 'form',
[Symbol.for('children')]: ['sometext', {/* some inputs... */}],
action: '/my/awesome/form',
method: 'POST'
};
},
mapsAndSets() {
const myMap = new Map([['a', 1], ['b', 2]]);
console.log(myMap); // Map {"a" => 1, "b" => 2}
const hello = {};
myMap.set(hello, 'world!');
console.log(myMap.get(hello)); // 'world!'
// get, set, delete, has, size, clear
const mySet = new Set([1, 1, 2]);
console.log(mySet); // Set {1, 2}
// ~ same set of methods
},
forOfLoops() {
for (let i of [1, 2, 3]) console.log(i); // 1 2 3
for (let i of [1, , 3]) console.log(i); // 1 undefined 3
for (let c of 'abc') console.log(c); // a b c
const myMap = new Map([['a', 1], ['b', 2]]);
for (let i of myMap) console.log(i); // ['a', 1] ['b', 2]
const mySet = new Set([1, 2]);
for (let i of mySet) console.log(i); // 1 2
// Works basically with any iterable
},
generators() {
function* myGenerator(firstValue) {
let sentValue = yield firstValue;
yield sentValue;
yield 2;
yield* [3, 4];
}
for (let i of myGenerator(0)) console.log(i); // 0 undefined 2 3 4
const myIterator = myGenerator('Hello');
console.log(myIterator.next()['value']); // Hello
console.log(myIterator.next('world!')['value']); // world!
// Redefine how to iterate over some object, or some class
const iterableObject = {
[Symbol.iterator]: function*() {yield 1; yield 2;}
};
for (let i of iterableObject) {
console.log(i); // 1 2
}
class IterableClass {
*generator() {
yield 1;
yield 2;
}
}
for (let i of new IterableClass()) {
console.log(i);
}
},
proxiesAndReflections() {
const myProxy = new Proxy({}, {
get(proxied, key, proxy) {
return key;
}
});
console.log(myProxy['Hello'], myProxy['world!']); // 'Hello world!'
const myDefaultArrayObject = new Proxy({}, {
get(proxied, key, proxy) {
if (! key in proxied) {
proxied[key] = [];
}
return proxied[key];
}
});
myDefaultArrayObject.a.push(1);
console.log(myDefaultArrayObject.a); // [1]
const {myRevocableProxy, revoke} = Proxy.revocable({}, {});
revoke();
const myUselessProxy = new Proxy({}, {
get: Reflect.get,
set: Reflect.set
});
myUselessProxy.a = 1;
console.log(myUselessProxy.a);
},
miscellaneous() {
console.log(0o10, 0O10); // 8 8
console.log(0b10, 0B10); // 2 2
console.log(Array.from(new Set([1, 1, 2]))); // [1, 2]
console.log([,,,].fill('na')); // ?
},
};