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

<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 ]
博客
v8worker
05-08 2890
05-06 2869
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值