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

版权声明:版权归作者所有 https://blog.csdn.net/qq_36772866/article/details/88090828

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

<script src="https://unpkg.com/30-seconds-of-code@1/dist/_30s.es5.min.js"></script>
<script>
  _30s.average(1, 2, 3);
</script>
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]

在这里插入图片描述
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 ]

猜你喜欢

转载自blog.csdn.net/qq_36772866/article/details/88090828