30-seconds-of-code(持续更新学习中)

30秒理解一段小小的代码段
30-seconds-of-code
使用 npm 安装 30-seconds-of-code

npm install 30-seconds-of-code

使用 yarn 安装 30-seconds-of-code

yarn add 30-seconds-of-code

浏览器引入

Browser



Node

// CommonJS
const _30s = require('30-seconds-of-code');
_30s.average(1, 2, 3);

// ES Modules
import _30s from '30-seconds-of-code';
_30s.average(1, 2, 3);

Adapter:适配器
1 ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.
创建一个函数用于接收 n 个参数,忽略任何额外的参数

Call the provided function, fn, with up to n arguments, using Array.prototype.slice(0,n) and the spread operator (…).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));

应用

const firstTwoMax = ary(Math.max, 2);
[[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]

30-seconds-of-code(持续更新学习中)_第1张图片
call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);

例子

Promise.resolve([1, 2, 3])
  .then(call('map', x => 2 * x))
  .then(console.log); // [ 2, 4, 6 ]
const map = call.bind(null, 'map');
Promise.resolve([1, 2, 3])
  .then(map(x => 2 * x))
  .then(console.log); // [ 2, 4, 6 ]

你可能感兴趣的:(30-seconds-of-code(持续更新学习中))