es6 还原 es5

const compose = f => g => x => f(g(x));


const f = compose (x => x * 4) (x => x + 3);

f(2) // 20


求一个化简后的compose!!!!!!.......还有为什么f(2)等于8


慕姐4208626
浏览 420回答 1
1回答

梵蒂冈之花

"use strict";var compose = function compose(f) {  return function (g) {    return function (x) {      return f(g(x));    };  };};var f = compose(function (x) {  return x * 4;})(function (x) {  return x + 3;});f(2);地址可能给compose的参数换个名字更阅读一些?var compose = function(a) {    return function(b) {        return function(c) {            return a(b(c));        };    };};var f = compose(function(x) {    return x * 4;})(function(x) {    return x + 3;});console.log(f(2));调用的话一共有三次var f = compose(function(x) {    return x * 4;})(function(x) {    return x + 3;});两次f(2)一次三次调用再拆为var f1=compose(function(x) {    return x * 4;})var f2 = f1(function(x) {    return x + 3;})f2(2);compose(function(x) {return x * 4;})即参数a为函数function(x) {return x * 4;}内部a(b(c))相当于对a调用内部参数x为b(c),得到function(b){return function(c){return b(c)*4}}此时 f1 = function(b){return function(c){return b(c)*4}}同样对f1的调用f1(function(x) {return x + 3;})相当与参数b为function(x){return x+3},内部b(c)相当于将c作为参数传入b中,得到function(c){return (c+3)*4}此时f2=function(c){return (c+3)*4},f2(2)=20有点绕,可以拿笔和纸写一下。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript