Hit L to toggle language
Jazyk lze přepnout klávesou L
Ondřej Žára, @0ndras
Buzzword showcase
Hitparáda buzzwordů
this
, global variables)this
, globální proměnné)console.log
)console.log
)function x() {
...
return y();
}
new
(as a homework),
(za domácí úkol), this
, for loops
function add(numbers) {
if (numbers.length > 2) {
return numbers[0] + add(numbers.slice(1));
} else {
return numbers[0] + numbers[1];
}
}
function plus(x, y) { return x + y; }
function add(numbers) {
return numbers.reduce(plus);
}
You should be at least marginally familiar with these
O těchto bychom již měli mít alespoň základní ponětí
Array.prototype.{forEach, map, filter}
Array.prototype.{some, every}
Array.prototype.{reduce, reduceRight}
Address book: an array of people, every person has several e-mails. Who is on the blacklist?
Pole osob v adresáři, každá má několik e-mailů. Kdo je na blacklistu?
return people.filter(person =>
person.emails.some(email =>
blacklist.includes(email)
)
})
const str = "rgb(1, 5, 10)";
let [r,g,b] = str.match(/\d+/g).map(parseInt); // 1, NaN, 2
/*
parseInt("1", 0, arr);
parseInt("5", 1, arr);
parseInt("10", 2, arr);
*/
let [r,g,b] = str.match(/\d+/g).map(Number); // 1, 5, 10
Object.keys
, Object.values
, Object.entries
Array.from
to convert from querySelectorAll
and similarArray.from
pro konverzi z querySelectorAll
a podobných[10, 1, 2].sort(); // [1, 10, 2] ‽
[10, 1, 2].sort( (a,b) => a-b ); // [1, 2, 10]
DATA.sort((a, b) => {
return a.name.localeCompare(b.name)
});
"Hello %s!".format("world");
String.prototype.format = function(...args) {
return this.replace(/%s/g, match => args.shift());
}
String.prototype.format = function(...args) {
return args.reduce((str, arg) => str.replace(/%s/, arg), this);
}
add x y = x + y
add2 = add 2
add2 3 = 5
function add(x, y) { return x + y; }
var add2 = add(2); // ???
var add2 = partial(add, 2); // no built-in support
function partial(func, ...args) {
return func.bind(null, ...args);
}
var add2 = partial(add, 2);
add2(3); // 5
// do not try .apply for large arrays
function min(array) {
return array.reduce( (a, b) => Math.min(a, b) );
}
function minAge(array) {
return array.reduce( (a, b) => a.age < b.age ? a : b );
}
function clone(value) {
if (value instanceof Array) {
return value.map(clone);
} else if (value instanceof Object) {
return Object.entries(obj).reduce(merge, {});
} else return value;
}
function merge(object, [key, value]) {
let partial = { [key]: clone(value) };
return Object.assign(object, partial);
}
function myNew(ctor, ...args) {
let inst = Object.create(ctor.prototype);
let result = ctor.apply(inst, args);
return (typeof(result) == "object" ? result : inst);
}