绑定对象的函数里的 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

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

Creates a new object from the combination of two or more objects.

Use Array.prototype.reduce() combined with Object.keys(obj) to iterate over all objects and keys.
Use hasOwnProperty() and Array.prototype.concat() to append values for keys existing in multiple objects.

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

Creates an array of key-value pair arrays from an object or other iterable (object, array, string, set etc.).

Check if Symbol.iterator is defined and, if so, use Array.prototype.entries() to get an iterator for the given iterable, Array.from() to convert the result to an array of key-value pair arrays.
If Symbol.iterator is not defined for obj, use Object.entries() instead.

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

Compares two objects to determine if the first one contains equivalent property values to the second one.

Use Object.keys(source) to get all the keys of the second object, then Array.prototype.every(), Object.hasOwnProperty() and strict comparison to determine if all keys exist in the first object and have the same values.

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

Given a predicate function and a prop string, this curried function will then take an object to inspect by calling the property and passing it to the predicate.

Summon prop on obj, pass it to a provided predicate function and return a masked boolean.

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

Returns the target value in a nested JSON object, based on the given key.

Use the in operator to check if target exists in obj.
If found, return the value of obj[target], otherwise use Object.values(obj) and Array.prototype.reduce() to recursively call dig on each nested object until the first matching key/value pair is found.

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