成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

30-seconds-code——adapter

dcr309duan / 3088人閱讀

摘要:英文文章來源于給定一個鍵值和一組參數(shù),但給定一個上下文時調(diào)用它們。

英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md

Adapter call

給定一個鍵值和一組參數(shù),但給定一個上下文時調(diào)用它們。

使用閉包調(diào)用存儲的鍵值與存儲的參數(shù)

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 ]
*/
collectInto

改變一個可以接受數(shù)組為參數(shù)的函數(shù)成為可變量函數(shù).

給定一個函數(shù),它返回一個把所有傳入的參數(shù)轉(zhuǎn)變?yōu)閿?shù)組的閉包函數(shù).

const collectInto = fn => ( ...args ) => fn( args );
/*
const Pall = collectInto( Promise.all.bind(Promise) )
let p1 = Promise.resolve(1)
let p2 = Promise.resolve(2)
let p3 = new Promise((resolve) => setTimeout(resolve,2000,3))
Pall(p1, p2, p3).then(console.log)
*/
flip

Flip 利用一個函數(shù)作為參數(shù),然后降低一個參數(shù)轉(zhuǎn)變?yōu)樽詈笠粋€參數(shù)

用一個閉包接受可變參數(shù)的輸入, 在使用出第一個參數(shù)外的其余參數(shù)前先拼接第一個參數(shù)使它成為最后一個參數(shù).

const flip = fn => (...args) => fn(args.pop(), ...args)
/*
let a = {name: "John Smith"}
let b = {}
const mergeFrom = flip(Object.assign)
let mergePerson = mergeFrom.bind(a)
mergePerson(b) // == b
b = {}
Object.assign(b, a) // == b
*/
pipeFunctions

從左到右的執(zhí)行函數(shù)組合.

Array.reduce() 和 spread (...) 操作符來實現(xiàn)從左向右的執(zhí)行函數(shù)組合. 第一個函數(shù)可以接受任意個參數(shù),其余的只能接受一個參數(shù).

const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
/*
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = pipeFunctions(multiply, add5)
multiplyAndAdd5(5, 2) -> 15
*/
promisify

轉(zhuǎn)化一個返回 promise 的異步函數(shù).

返回一個函數(shù),它返回一個調(diào)用所有原始函數(shù)的 Promise .
...rest 去傳遞輸入的參數(shù).

在 Node 8+ 中, 你可以用 util.promisify

const promisify = func =>
  (...args) =>
    new Promise((resolve, reject) =>
      func(...args, (err, result) =>
        err ? reject(err) : resolve(result))
    );
// const delay = promisify((d, cb) => setTimeout(cb, d))
// delay(2000).then(() => console.log("Hi!")) -> Promise resolves after 2s
spreadOver

用一個可變參數(shù)的函數(shù)并返回一個閉包,該閉包可以將輸入的數(shù)組參數(shù)的轉(zhuǎn)變?yōu)閰?shù)序列.

用閉包和spread (...) 操作符去映射一個函數(shù)的參數(shù)數(shù)組.

const spreadOver = fn => argsArr => fn(...argsArr);
/*
const arrayMax = spreadOver(Math.max)
arrayMax([1,2,3]) // -> 3
arrayMax([1,2,4]) // -> 4
*/

更多關于30-seconds-code中文翻譯https://github.com/lvzhenbang/article/blob/master/js/30-seconds-code/index.md

文章版權歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/90529.html

相關文章

  • 30-seconds-code——Object

    摘要:英文文章來源于刪除對象中除指定鍵值的屬性用遞歸的方法用方法遍歷對象然后刪除不是在給定數(shù)組中的屬性如果你傳入,它將對該鍵所對應的對象進行深度遍歷的變形非原著作對所有的鍵對應的對象進行深度遍歷用方法遍歷對象然后刪除不是在給定數(shù)組中的屬性如 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/READM...

    lbool 評論0 收藏0
  • 30-seconds-code——string

    摘要:英文文章來源于計算一個字符串中字符的所有排序情況使用遞歸遍歷字符串中的每個字符計算剩余字符串的所有順序用區(qū)合并該字符和剩余字符串的每種順序然后用將該字符串的所有順序合并到一個數(shù)組中當字符串的等于或者時,是兩個基例字符串的首字母大寫用 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README...

    tinysun1234 評論0 收藏0
  • 30-seconds-code ——utility集合

    摘要:英文文章來源于返回參數(shù)列表中第一個非和的參數(shù)用實現(xiàn)返回第一個非參數(shù)返回一個用自定義函數(shù)中的函數(shù)是否返回來對中傳入的參數(shù)列表盡心過濾用去遍歷參數(shù)列表,用給定的函數(shù)的返回值來過濾參數(shù)列表返回給定值的基本類型返回給定值的構造函數(shù)名字的小 Utility 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master...

    Jochen 評論0 收藏0
  • 30-seconds-code——browser

    摘要:顯示所有指定的元素用操作符和清除所有指定元素的屬性。使用了兩個事件監(jiān)聽器。將指定的數(shù)組元素轉(zhuǎn)換成元素標簽,然后將它們插入指定的選擇器元素內(nèi)用和去生成一個元素標簽列表復制一個字符串到剪切板。用去執(zhí)行復制到剪切板。 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md Browser...

    izhuhaodev 評論0 收藏0
  • 30-seconds-code——array

    摘要:英文文章來源于數(shù)組最大公約數(shù)計算數(shù)字數(shù)組最大公約數(shù)用和運算式使用遞歸計算一個數(shù)字數(shù)組的最大公約數(shù)數(shù)組最小公倍數(shù)求數(shù)字數(shù)組的最小公倍數(shù)用和運算式使用遞歸計算一個數(shù)字數(shù)組的最小公倍數(shù)返回一個數(shù)組中的最大值。 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md Array 數(shù)組最大公...

    adie 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<