30秒理解一段小小的代码段
使用 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]
复制代码