Partial application using .bind()
is probably my favourite.
Example:
function add(a, b, c, d) { return a + b + c + d; }
// returns a function that has an airity of one (d) and has a, b and c partially applied
var partiallyAppliedAdd = add.bind(null, 1, 2, 3);
var x = add(1,2,3,4); // 10
var y = partiallyAppliedAdd(4); // 10
assert(x, y); // true
also these are pretty nice.
// a double bitwise not operator acts like Math.floor
var x = Math.floor(10.9); // 10
var y = ~~10.9; // 10
assert(x, y); // true
// prefixing new Date() with a '+' will give you a unix timestamp
var z = +new Date(); // 1418763280491 at the time of writing this
// in es6 - pattern matching
var person = {fname: "Bob", lname: "The Builder", age: "old"};
var {fname, lname} = person;
assert(fname, person.fname) // true
assert(lname, person.lname) // true
and of course
// javascript being functional
function sqr(x) { return x * x; }
function doub(x) { return x + x; }
function add5(x) { return x + 5; }
function add(x, y) { return x + y; }
function boop(value) {
return function(fn) {
return fn(value);
}
}
[sqr, doub, add5, add.bind(null,1)].map(boop(5)); // [25, 10, 10, 6]