http://scotthurff.com/posts/how-to-design-for-thumbs-in-the-era-of-huge-screens
Koan es6katas, 57: Default parameters – basics
// 57: Default parameters - basics
// To do: make all tests pass, leave the assert lines unchanged!
describe('default parameters make function parameters more flexible', () => {
it('define it using an assignment to the parameter `function(param=1){}`', function() {
let number = (int=0) => int;
assert.equal(number(), 0);
});
it('it is used when undefined is passed', function() {
let number = (int = 23) => int;
const param = undefined;
assert.equal(number(param), 23);
});
it('it is not used when a value is given', function() {
function xhr(method) {
return method;
}
assert.equal(xhr('POST'), 'POST');
});
it('it is evaluated at run time', function() {
let defaultValue = 42;
function xhr(method = `value: ${defaultValue}`) {
return method;
}
assert.equal(xhr(), 'value: 42');
defaultValue = 23;
});
it('it can also be a function', function() {
function defaultValue(){};
function fn(value = defaultValue()) {
return value;
}
assert.equal(fn(), defaultValue());
});
});
Koan es6katas, 8: block scope – const
// 8: block scope - const
// To do: make all tests pass, leave the asserts unchanged!
describe('`const` is like `let` plus read-only', () => {
describe('scalar values are read-only', () => {
it('number', () => {
const constNum = 0;
//constNum = 1;
assert.equal(constNum, 0);
});
it('string', () => {
const constString = 'I am a const';
//constString = 'Cant change you?';
assert.equal(constString, 'I am a const');
});
});
const notChangeable = 23;
it('const scope leaks too', () => {
assert.equal(notChangeable, 23);
});
describe('complex types are NOT fully read-only', () => {
it('array', () => {
const arr = [42, 23];
//arr[0] = 0;
assert.equal(arr[0], 42);
});
it('object', () => {
const obj = {x: 1};
obj.x = 3;
assert.equal(obj.x, 3);
});
});
});
Koan es6katas, 7: block scope – let
// 7: block scope - let
// To do: make all tests pass, leave the asserts unchanged!
describe('`let` restricts the scope of the variable to the current block', () => {
describe('`let` vs. `var`', () => {
it('`var` works as usual', () => {
if (true) {
var varX = true;
}
assert.equal(varX, true);
});
it('`let` restricts scope to inside the block', () => {
if (true) {
let letX = true;
}
assert.throws(() => console.log(letX));
});
});
describe('`let` usage', () => {
it('`let` use in `for` loops', () => {
let obj = {x: 1};
for (let key in obj) {}
assert.throws(() => console.log(key));
});
it('create artifical scope, using curly braces', () => {
{
let letX = true;
}
assert.throws(() => console.log(letX));
});
});
});
Koan es6katas, 6: arrow functions – binding**
// 6: arrow functions - binding
// To do: make all tests pass, leave the asserts unchanged!
class LexicallyBound {
getFunction() {
return () => {
return this;
}
}
getArgumentsFunction() {
return () => {
return arguments
}
}
}
describe('arrow functions have lexical `this`, no dynamic `this`', () => {
it('bound at definition time, use `=>` ', function() {
var bound = new LexicallyBound();
var fn = bound.getFunction();
assert.strictEqual(fn(), bound);
});
it('can NOT bind a different context', function() {
var bound = new LexicallyBound();
var fn = bound.getFunction();
var anotherObj = bound;
var expected = anotherObj;
assert.strictEqual(fn.call(anotherObj), expected);
});
it('`arguments` doesnt work inside arrow functions', function() {
var bound = new LexicallyBound();
var fn = bound.getArgumentsFunction();
assert.equal(fn(1, 2).length, 0);
});
});
Koan es6katas, 5: arrow functions – basics
// 5: arrow functions - basics
// To do: make all tests pass, leave the asserts unchanged!
describe('arrow functions', function() {
it('are shorter to write', function() {
var func = () => 'I am func';
assert.equal(func(), 'I am func');
});
it('a single expression, without curly braces returns too', function() {
var func = () => 'I return too';
assert.equal(func(), 'I return too');
});
it('one parameter can be written without parens', () => {
var func = p => p - 1;
function func2(val){
return val - 1;
}
assert.equal(func(25), 24);
assert.equal(func2(25), 24);
});
it('many params require parens', () => {
var func = (param,param1) => param + param1;
assert.equal(func(23, 42), 23+42);
});
it('body needs parens to return an object', () => {
var func = () => ({iAm: 'an object'});
assert.deepEqual(func(), {iAm: 'an object'});
});
});
Koan es6katas, 21: spread – with-strings
// 21: spread - with-strings
// To do: make all tests pass, leave the assert lines unchanged!
describe('spread with strings', () => {
it('simply spread each char of a string', function() {
const [a, b] = [...'ab'];
assert.equal(a, 'a');
assert.equal(b, 'b');
});
it('extracts each array item', function() {
const [a,b,c] = [...'12'];
assert.equal(a, 1);
assert.equal(b, 2);
});
it('works anywhere inside an array (must not be last)', function() {
const letters = ['a', ...'bcd', 'e', 'f'];
assert.equal(letters.length, 6);
});
it('dont confuse with the rest operator', function() {
const [...rest] = [...'1234', '5'];
assert.deepEqual(rest, [1, 2, 3, 4, 5]);
});
it('passed as function parameter', function() {
const max = Math.max(...[1,2,3,4,5]);
assert.deepEqual(max, 5);
});
});
Koan es6katas, 20: spread – with-arrays
// 20: spread - with-arrays
// To do: make all tests pass, leave the assert lines unchanged!
describe('spread with arrays', () => {
it('extracts each array item', function() {
const [a, b] = [...[1, 2]];
assert.equal(a, 1);
assert.equal(b, 2);
});
it('in combination with rest', function() {
const [a, b, ...rest] = [...[1, 2, 3, 4, 5]];
assert.equal(a, 1);
assert.equal(b, 2);
assert.deepEqual(rest, [3, 4, 5]);
});
it('spreading into the rest', function() {
const [...rest] = [...[1, 2, 3, 4, 5]];
assert.deepEqual(rest, [1, 2, 3, 4, 5]);
});
describe('used as function parameter', () => {
it('prefix with `...` to spread as function params', function() {
const magicNumbers = [1, 2];
const fn = (magicA, magicB) => {
assert.deepEqual(magicNumbers[0], magicA);
assert.deepEqual(magicNumbers[1], magicB);
};
fn(...magicNumbers);
});
it('pass an array of numbers to Math.max()', function() {
const max = Math.max(...[23, 0, 42, 41]);
assert.equal(max, 42);
});
});
});
Koan es6katas, 19: rest – with-destructuring
// 19: rest - with-destructuring
// To do: make all tests pass, leave the assert lines unchanged!
describe('rest with destructuring', () => {
it('rest parameter must be last', () => {
const [...all] = [1, 2, 3, 4];
assert.deepEqual(all, [1, 2, 3, 4]);
});
it('assign rest of an array to a variable', () => {
const [first,...all] = [1, 2, 3, 4];
assert.deepEqual(all, [2, 3, 4]);
});
// the following are actually using `spread` ... oops, to be fixed
it('concat differently', () => {
const theEnd = [3, 4];
const allInOne = [1, 2, ...theEnd];
assert.deepEqual(allInOne, [1, 2, 3, 4]);
});
it('`apply` made simple, even for constructors', () => {
const theDate = [2015, 1, 1];
const date = new Date(...theDate);
console.log("theDate-->",theDate);
console.log("...theDate-->",...theDate);
assert.deepEqual(new Date(2015, 1, 1), date);
});
});
Asignación por destructuring en Javascript
let [var1,var2] = [44,42];
console.log("var1-->",var1);
console.log("var2-->",var2);
https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Operadores/Destructuring_assignment

