当前位置:   article > 正文

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

30-seconds-of-code

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

npm install 30-seconds-of-code
  • 1

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

yarn add 30-seconds-of-code
  • 1

浏览器引入

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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));
  • 1

应用

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

在这里插入图片描述
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);
  • 1

例子

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 ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/685943
推荐阅读
相关标签
  

闽ICP备14008679号