const compose = (...fns) => {
let len = fns.length;
let fn_index = len - 1;
let fn_result; function invoke(...args) {
fn_result = len ? fns[fn_index].apply(this, args) : args[0]
if(fn_index <= 0){
fn_index = len - 1;
return fn_result;
}else{
fn_index--;
return invoke.call(null, fn_result);
}
} return invoke;
} const compose2 = (...fns) => {
const length = fns.length;
let index = length;
while(index--){
if(typeof(fns[index]) !== 'function'){
throw new TypeError(`fns ${index} not a function.`)
}
} return (...args) => {
let index = 0;
let result = length ? fns[index].apply(this, args) : args[0]
while(++index < length) {
result = fns[index].call(this, result)
}
return result;
}
} const compose2Right = (...fns) => {
return compose2.apply(null, fns.reverse())
} const upperName = compose(str => str.toUpperCase(), (firstName, lastName) => firstName + ',' + lastName)
const name = upperName('flex', 'react')
console.log(name) const upperName2 = compose2((firstName, lastName) => firstName + ',' + lastName, str => str.toUpperCase())
const name2 = upperName2('flex', 'react')
console.log(name2) const upperName3 = compose2Right(str => str.toUpperCase(), (firstName, lastName) => firstName + ',' + lastName)
const name3 = upperName3('flex', 'react')
console.log(name3) const upperName4 = compose2()
const name4 = upperName4('a','b')
console.log(name4) const upperName5 = compose()
const name5 = upperName5('a','b')
console.log(name5)

es6 nodejs compose的更多相关文章

  1. 如何在NodeJS项目中优雅的使用ES6

    如何在NodeJS项目中优雅的使用ES6 NodeJs最近的版本都开始支持ES6(ES2015)的新特性了,设置已经支持了async/await这样的更高级的特性.只是在使用的时候需要在node后面加 ...

  2. NPM 与 Nodejs

    安装了Nodejs之后,NPM也安装好了 如何知道当前是否已经安装Nodejs和NPM了呢? node -v //查看当前nodejs的版本 npm -v //查看当前npm的版本 NPM 初始化 n ...

  3. 前端面试题总结:HTML5,JS,CSS3,兼容性。

    1. 请写出至少20个HTML5标签 <article><aside> <audio><video> <canvas><datalis ...

  4. Mac新手从入门到放弃MongoDB

    1. 简介 MongoDB 是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最 ...

  5. 编写JavaScript 代码的5个小技巧

    1.Array.includes 与条件判断 一般我们判断或用 || // condition function test(fruit) { if (fruit == "apple" ...

  6. 2016年后web开发趋势是什么?

    2016 年后 Web开发趋势是什么 来源:yafeilee.me 发布时间:2016-05-06 阅读次数:1378 3   近二年的进展 前端发展日新月异, 甚至有一句戏言: "每六星期 ...

  7. NuxtJS快速入门

    服务器端渲染(SSR) 知识储备 ES6 Nodejs Vue React Angular 什么是服务器端渲染 前端渲染:html页面作为静态文件存在,前端请求时后端不对该文件做任何内容上的修改,直接 ...

  8. 让nodeJS支持ES6的词法----babel的安装和使用

    要使用Babel, 我们需要nodeJS的环境和npm, 主要安装了nodeJS, npm就默认安装了 , 现在安装nodeJS很简单了, 直接下载安装就好了: 安装es-checker 在使用Bab ...

  9. 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)

    请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...

随机推荐

  1. ORM到底是用还是不用?(复制)

    ORM即Object/Relation Mapping的简写,一般称作“对象关系映射”,在Web开发中最常出没于和关系型数据库交互的地方.接口.中间件.库.包,你都可以这么称呼它.ORM我们可以结合P ...

  2. php处理restful请求的路由(转载 http://www.jb51.net/article/47333.htm)

    <?php    class Router {        // 路由表        private $routers = array(            array("nam ...

  3. 数据库(11)-- Hash索引和BTree索引 的区别

    索引是帮助mysql获取数据的数据结构.最常见的索引是Btree索引和Hash索引. 不同的引擎对于索引有不同的支持:Innodb和MyISAM默认的索引是Btree索引:而Mermory默认的索引是 ...

  4. [笔记] Python实现全排列算法

    所谓全排列,就是给定数组,将所有的可能排列组合都枚举出来,n个元素共有n!种排列组合. 举例,对于['1', '2', '3'],全排列结果为:123,132,213,231,312,321,共有3! ...

  5. v9上传图片/附件失败出现undefined的解决方法之一

    把phpcms\modules\attachment\attachments.php中将                        if(empty($this->userid)){改成  ...

  6. 字符串的最小最大表示法O(n)

    以下介绍内容内容转自:http://blog.csdn.net/zy691357966/article/details/39854359 网上看了这篇文章后还是感觉有些地方讲的没有详细的证明所以添加了 ...

  7. hdp (ambari) 集成hue

    ambari-server resetambari-admin-password-reset https://github.com/EsharEditor/ambari-hue-service可以基于 ...

  8. SqlHelper简单实现(通过Expression和反射)10.使用方式

    以下是整个SqlHelper的Demo: public Result<List<ArticleDTO>> GetIndexArticleList(int count, int ...

  9. Spring七大框架

    Spring Core:最基础部分,提供IOC和依赖注入.基础概念是BeanFactory,提供对Factory模式的经典实现,这样来消除对程序性单例模式的需要,并真正地允许你从程序逻辑中分离出依赖关 ...

  10. Remote System Explorer Operation总是运行后台服务,卡死eclipse

    阿里云 > 教程中心 > android教程 > Remote System Explorer Operation总是运行后台服务,卡死eclipse Remote System E ...