绑定对象的函数里的 this 到对象本身。

使用 Array.prototype.forEach() 来遍历对象里所有指定的函数,使用 Function.prototype.apply() 来指定函数中 this 的上下文为对象本身,再覆盖原先的函数。

继续阅读 30秒学会 JavaScript 片段 – bindAll

Groups the elements into two arrays, depending on the provided function’s truthiness for each element.

Use Array.prototype.reduce() to create an array of two arrays.
Use Array.prototype.push() to add elements for which fn returns true to the first array and elements for which fn returns false to the second one.

继续阅读 30秒学会 JavaScript 片段 – partition

Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.

Use Array.prototype.map() to reorder arguments based on indexes in combination with the spread operator (...) to pass the transformed arguments to fn.

继续阅读 30秒学会 JavaScript 片段 – rearg

Curries a function.

Use recursion.
If the number of provided arguments (args) is sufficient, call the passed function fn.
Otherwise, return a curried function fn that expects the rest of the arguments.
If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. Math.min()), you can optionally pass the number of arguments to the second parameter arity.

继续阅读 30秒学会 JavaScript 片段 – curry

Removes elements in an array until the passed function returns true. Returns the removed elements.

Loop through the array, using a for...of loop over Array.prototype.entries() until the returned value from the function is true.
Return the removed elements, using Array.prototype.slice().

继续阅读 30秒学会 JavaScript 片段 – takeWhile

Removes elements from the end of an array until the passed function returns true. Returns the removed elements.

Loop through the array, using a Array.prototype.reduceRight() and accumulating elements while the function returns falsy value.

继续阅读 30秒学会 JavaScript 片段 – takeRightWhile

Returns an array of function property names from own (and optionally inherited) enumerable properties of an object.

Use Object.keys(obj) to iterate over the object’s own properties.
If inherited is true, use Object.get.PrototypeOf(obj) to also get the object’s inherited properties.
Use Array.prototype.filter() to keep only those properties that are functions.
Omit the second argument, inherited, to not include inherited properties by default.

继续阅读 30秒学会 JavaScript 片段 – functions

Creates an object composed of the properties the given function returns falsy for. The function is invoked with two arguments: (value, key).

Use Object.keys(obj) and Array.prototype.filter()to remove the keys for which fn returns a truthy value.
Use Array.prototype.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.

继续阅读 30秒学会 JavaScript 片段 – omitBy