{"id":411,"date":"2022-06-24T09:37:51","date_gmt":"2022-06-24T09:37:51","guid":{"rendered":"http:\/\/114.132.224.28\/?p=411"},"modified":"2022-06-24T09:37:51","modified_gmt":"2022-06-24T09:37:51","slug":"%e4%b8%80%e6%ad%a5%e6%ad%a5%e5%ae%9e%e7%8e%b0%e4%b8%ad%e9%97%b4%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/localhost\/2022\/06\/24\/%e4%b8%80%e6%ad%a5%e6%ad%a5%e5%ae%9e%e7%8e%b0%e4%b8%ad%e9%97%b4%e4%bb%b6\/","title":{"rendered":"\u4e00\u6b65\u6b65\u5b9e\u73b0\u4e2d\u95f4\u4ef6"},"content":{"rendered":"\n
\u4e2d\u95f4\u4ef6\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898\uff1a<\/p>\n\n\n\n
\u5e95\u5c42\u903b\u8f91\u548c\u4e1a\u52a1\u903b\u8f91\u5e94\u8be5\u662f\u5206\u5f00\u7684\uff0c\u5141\u8bb8\u8c03\u7528\u8005\u5c06\u4e00\u4e9b\u903b\u8f91\u63d2\u5165\u5230\u5185\u90e8\u91cc\u9762\u8fd0\u884c\uff0c\u65b0\u589e\u63d2\u4ef6\u65f6\u5185\u90e8\u4e0d\u4f1a\u6536\u5230\u5f71\u54cd<\/p>\n\n\n\n
\u5728\u8bbe\u8ba1\u7684\u65f6\u5019\uff0c\u5c06\u4e00\u4e9b\u63d2\u4ef6\uff08\u4e1a\u52a1\u903b\u8f91\uff09\u5f15\u5165\u5230\u6a21\u5757\u91cc\uff0c\u8fde\u63a5\u5e95\u5c42\u670d\u52a1\u4e0e\u5e94\u7528\u4e1a\u52a1\u903b\u8f91\uff0c\u6bd4\u5982express\uff0c\u5728\u63a5\u53d7\u5230\u8bf7\u6c42\u540e\uff0c\u53d1\u9001\u8bf7\u6c42\u524d\uff0c\u6267\u884c\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u6bd4\u5982react\u4e2d\uff0c\u5728\u8c03\u7528reduce\u524d\uff0cdispatch\u540e\uff0c\u8fd9\u4e00\u4e9b\u4e2d\u95f4\u4ef6\u9700\u8981\u94fe\u5f0f\u8c03\u7528<\/p>\n\n\n\n
\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n\n\n\n
\/\/ 1. \u6570\u7ec4\nfunction arrayPattern () {\n const middleware = []\n function output (...args) {\n console.log(...args)\n }\n function use (fn) {\n middleware.push(fn)\n }\n function input (...args) {\n middleware.forEach(m => m(...args))\n output(...args)\n }\n\n use(x => { x.data += 1 })\n input({ data: 1 })\n}\narrayPattern()\n\n\/\/ 1.1 \u52a0\u5165next\u63a7\u5236\nfunction nextPlus () {\n const middleware = []\n function use (...fn) {\n middleware.push(...fn)\n }\n function input (...args) {\n function iterator (index) {\n if (index === middleware.length) return\n return middleware[index](...args, () => iterator(index + 1))\n }\n iterator(0)\n output(...args)\n }\n function output (...args) {\n console.log(...args)\n }\n use((ctx, next) => { ctx.data += 1; next() }, (ctx, next) => { ctx.data *= 2; next() })\n input({ data: 1 })\n}\nnextPlus()\n\n\/\/ 1.2 with err\nfunction noNextPlus () {\n const middleware = []\n function use (...fn) {\n middleware.push(...fn)\n }\n function input (...args) {\n function iterator (index) {\n if (index === middleware.length) return\n return middleware[index](...args, () => iterator(index + 1))\n }\n iterator(0)\n output(...args)\n }\n function output (...args) {\n console.log(...args)\n }\n use((ctx, next) => { ctx.data += 1 }, (ctx, next) => { ctx.data *= 2; next() })\n input({ data: 1 })\n}\nnoNextPlus()\n\n\/\/ 2.\u589e\u5f3aInput\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u662f\u5148\u6267\u884c\u6700\u65b0\u7684\u518d\u6267\u884c\u65e7\u7684\nfunction decorateInput () {\n function input (...args) {\n output(...args)\n }\n function output (...args) {\n console.log(...args)\n }\n function composeInput (fn, input) {\n return fn.reduce((pre, cur) => (...args) => {\n pre(cur(...args))\n }, input)\n }\n composeInput([x => x + 1], input)(1)\n composeInput([x => x + 1, x => x * 2], input)(1)\n}\ndecorateInput()\n\n\/\/ 3. \u76f4\u63a5\u628ainput\u4e5f\u4f20\u8fdb\u53bb\uff0c\u5e76\u4e14\u8fd4\u56de\u51fd\u6570\u800c\u4e0d\u662f\u76f4\u63a5\u6267\u884c,\u6b64\u65f6\u6700\u5916\u5c42\u7684\u51fd\u6570\u662f\u65e7\u7684\uff0c\u6240\u4ee5\u4f1a\u65e7\u7684\u6700\u5148\u5f97\u5230\u53c2\u6570\uff0c\u7136\u540e\u518d\u6267\u884c\u65b0\u7684\u51fd\u6570\nfunction inputWithFun () {\n function input (...args) {\n output(...args)\n }\n function output (...args) {\n console.log(...args)\n }\n function composeInputBoth (fn) {\n return fn.reduce((pre, cur) => (...args) => pre(cur(...args)))\n }\n composeInputBoth([input => x => input(x + 1), input => x => input(x * 2)])(input)(1)\n\n \/\/ 4.\u8c03\u6574\u8f93\u5165\u53ef\u4ee5\u63a5\u53d7\u51fd\u6570\u6216\u4e00\u822c\u503c\n composeInputBoth([input => x => typeof x === 'function' ? input(x() + 1) : input(x + 1), input => x => input(x * 2)])(input)(() => 1)\n}\ninputWithFun()\n<\/code><\/pre>\n\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"
\u4e2d\u95f4\u4ef6\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898\uff1a \u5e95\u5c42\u903b\u8f91\u548c\u4e1a\u52a1\u903b\u8f91\u5e94\u8be5\u662f\u5206\u5f00 …<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts\/411"}],"collection":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/comments?post=411"}],"version-history":[{"count":0,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts\/411\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/media?parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/categories?post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/tags?post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}