Skip to content

Instantly share code, notes, and snippets.

@alexspark
Created December 15, 2017 04:33
Show Gist options
  • Save alexspark/765a8bb6963484a8895ea724491f0973 to your computer and use it in GitHub Desktop.
Save alexspark/765a8bb6963484a8895ea724491f0973 to your computer and use it in GitHub Desktop.
learnES6 created by alexspark - https://repl.it/@alexspark/learnES6
// constants
const NAME = `Alex Park`
console.log(`const: ${NAME}`)
// block scope: var vs let
// best explanation so far:
// https://www.sitepoint.com/joys-block-scoping-es6/
for (var i=0; i<3; i++) {
var j = i * i
}
console.log(`hoisted var j: ${j}`)
let inf = Infinity
for (var i=0;i<3;i++) {
let inf = i * i
}
console.log(`unhoisted let inf: ${inf}`);
let callbacks = [];
for (var i=0;i<3;i++) {
callbacks[i] = function () { return i * 2 }
}
console.log(`
callbacks[] invoked when using var i...
callbacks[0] = ${callbacks[0]()}
callbacks[1] = ${callbacks[1]()}
callbacks[2] = ${callbacks[2]()}
`);
callbacks = []
for (let i=0;i<3;i++) {
callbacks[i] = function () { return i * 2 }
}
console.log(`
callbacks[] invoked when using let i...
callbacks[0] = ${callbacks[0]()}
callbacks[1] = ${callbacks[1]()}
callbacks[2] = ${callbacks[2]()}
`);
// lexical scope with blocks!
{
function fn() { return 1 }
console.log(`fn() === 1: ${fn() === 1}`)
{
function fn() { return 2 }
console.log(`fn() === 2: ${fn() === 2}`)
}
console.log(`fn() === 1: ${fn() === 1}`)
}
let m = [0, 1, 2, 3, 4, 5].map(i => i * 2);
console.log(`m: ${m}`);
m = [0, 1, 2, 3, 4, 5].map((i) => ({ number: i, next: i+1 }));
console.log(Object.values(m));
let context = {
replyTo: '[email protected]',
friends: ['anna', 'louise'],
broadcast() {
this.friends.forEach(f => {
console.log(`${this.replyTo}: ${f}`)
})
}
}
console.log(context.broadcast());
function square() {
const example = () => {
let numbers = []
for (number of arguments) {
numbers.push(number * number)
}
return numbers;
}
return example()
}
console.log(`
lexical \`arguments\`:
${square(1, 2, 4, 9)}
`);
const power2defaulted = (x=1) => Math.pow(2, x);
console.log(`
power2defaulted(): ${power2defaulted()}
power2defaulted(2): ${power2defaulted(2)}
`);
const restAdd50 = (...r) => {
return r.reduce((r, acc) => r + acc, 50)
};
console.log(`
restAdd50(10, 20, 30) => ${restAdd50(10, 20, 30)}
`);
const firstShallBeLast = arr => [arr[arr.length-1], ...arr.slice(0, arr.length-1)];
console.log(`
firstShallBeLast([1, 2, 3]) => [${firstShallBeLast([1, 2, 3])}]
`);
const asChars = word => [...word];
console.log(`
asChars("alex"): ${asChars("alex")}
`);
const shorthandVar = 29;
const shortObj = { shorthandVar };
console.log(`
shorthandVar: 29
shortObj = { shorthandVar };
shortObj.shorthandVar => ${shortObj.shorthandVar}
`);
const methodProps = {
meth1(a, b) { return a * b },
meth2(a, b) { return a - b }
};
console.log(`
methodProps.meth1(2, 3) => ${methodProps.meth1(2, 3)}
methodProps.meth2(2, 3) => ${methodProps.meth2(2, 3)}
`);
const destructList = [...'alex'];
let [first, , ,last] = destructList;
console.log(`
const destructList = [...'alex'];
let [first, , , last] = destructList;
first => ${first}
last => ${last}
`)
//[last, first] = [first, last];
//console.log(`
// [last, first] = [ first, last ];
// first => ${first}
// last => ${last}
//`)
const makeComputed = (name) => ({
name,
['print' + name]: function() {
return [...name].reverse().join('');
}
});
console.log(`
const makeComputed = (name) => ({
['print' + name]: function() {
return [...name].reverse().join('');
}
});
makeComputed('Alex').printAlex() => ${makeComputed('Alex').printAlex()}
`);
let { name } = makeComputed('eunice');
console.log(`
let { name } = makeComputed('eunice'); => ${name}
`);
class Cat {
constructor (name, breed, owner) {
this._name = name;
this.owner = owner;
this.breed = breed;
this.meowCount = 1;
}
hi() {
return `what do you want, ${this.owner}!? ehhh!?`
}
get name() { return this._name; }
set name(n) { this._name = n; }
static m () { return 'MEOWMEOWMEOW!!!!'; }
}
const cat = new Cat('Bella', 'egyptian cat', 'alex');
console.log(`
const cat = new Cat('Bella', 'egyptian cat', 'alex');
cat.hi() => ${cat.hi()}
`);
cat.name = 'eunice';
console.log(`
cat.name = 'eunice'
cat.name => ${cat.name}
Cat.m() => ${Cat.m()}
`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment