{"id":223,"date":"2025-02-04T22:25:00","date_gmt":"2025-02-04T14:25:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=223"},"modified":"2026-06-05T10:21:42","modified_gmt":"2026-06-05T02:21:42","slug":"%e5%87%bd%e6%95%b0%e6%9f%af%e9%87%8c%e5%8c%96","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2025\/02\/04\/%e5%87%bd%e6%95%b0%e6%9f%af%e9%87%8c%e5%8c%96\/","title":{"rendered":"\u51fd\u6570\u67ef\u91cc\u5316"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u67ef\u91cc\u5316\uff08\u82f1\u8bed\uff1a<em>Currying<\/em>\uff09\uff0c\u53c8\u8bd1\u4e3a\u5361\u745e\u5316\u6216\u52a0\u91cc\u5316\uff0c\u662f\u628a\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\u7684\u51fd\u6570\u53d8\u6362\u6210\u63a5\u53d7\u4e00\u4e2a\u5355\u4e00\u53c2\u6570\uff08\u6700\u521d\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff09\u7684\u51fd\u6570\uff0c\u5e76\u4e14\u8fd4\u56de\u63a5\u53d7\u4f59\u4e0b\u7684\u53c2\u6570\u800c\u4e14\u8fd4\u56de\u7ed3\u679c\u7684\u65b0\u51fd\u6570\u7684\u6280\u672f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u6280\u672f\u7531\u514b\u91cc\u65af\u6258\u5f17\u00b7\u65af\u7279\u96f7\u5947\u4ee5\u903b\u8f91\u5b66\u5bb6\u54c8\u65af\u51ef\u5c14\u00b7\u52a0\u91cc\u547d\u540d\u7684\uff0c\u5c3d\u7ba1\u5b83\u662f <em>Moses Sch\u00f6nfinkel<\/em> \u548c\u6208\u7279\u6d1b\u5e03\u00b7\u5f17\u96f7\u683c\u53d1\u660e\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6c42\u53d6\u4e24\u4e2a\u6570\u4e4b\u548c\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function add(x, y) {<br> &nbsp; &nbsp;return x + y;<br>}<br>console.log(add(1, 2)); \/\/ 3<br>console.log(add(5, 7)); \/\/ 12<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a <em>add<\/em> \u51fd\u6570\uff0c\u63a5\u6536\u4e24\u4e2a\u5f62\u53c2\uff0c\u8fd4\u56de\u4e24\u5f62\u53c2\u7684\u548c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8c03\u7528\u7684\u65f6\u5019\uff0c\u6211\u4eec\u6bcf\u6b21\u4e5f\u9700\u8981\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\uff0c\u6211\u4eec\u5bf9\u5176\u8fdb\u884c\u67ef\u91cc\u5316\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function add(x) {<br> &nbsp; &nbsp;return function (y) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return x + y;<br> &nbsp;  }<br>}<br>console.log(add(1)(2)); \/\/ 3<br>console.log(add(5)(7)); \/\/ 12<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5bf9 <em>add<\/em> \u51fd\u6570\u8fdb\u884c\u4e86\u67ef\u91cc\u5316\u6539\u9020\uff0c\u53ea\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u4f46\u662f\u8fd4\u56de\u7684\u4e5f\u4e0d\u662f\u503c\u4e86\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e5f\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u7136\u540e\u5229\u7528\u95ed\u5305\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u8bbf\u95ee\u5230\u6700\u5f00\u59cb\u4f20\u5165\u7684 <em>x<\/em> \u7684\u503c\uff0c\u6700\u7ec8\u8fd4\u56de <em>x<\/em> \u548c <em>y<\/em> \u7684\u548c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u901a\u8fc7\u4e0a\u9762\u7684\u8fd9\u4e2a\u793a\u4f8b\uff0c\u6211\u4eec\u80fd\u591f\u4f53\u4f1a\u5230\u524d\u9762\u6240\u8bf4\u7684\u67ef\u91cc\u5316\u51fd\u6570\u7684\u7279\u70b9\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u67ef\u91cc\u5316\u7684\u51fd\u6570\u9996\u5148\u4f1a\u63a5\u53d7\u4e00\u4e9b\u53c2\u6570\uff0c\u63a5\u53d7\u4e86\u8fd9\u4e9b\u53c2\u6570\u4e4b\u540e\uff0c\u8be5\u51fd\u6570\u5e76\u4e0d\u4f1a\u7acb\u5373\u6c42\u503c\uff0c\u800c\u662f\u7ee7\u7eed\u8fd4\u56de\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\uff0c\u521a\u624d\u4f20\u5165\u7684\u53c2\u6570\u5728\u51fd\u6570\u5f62\u6210\u7684\u95ed\u5305\u4e2d\u88ab\u4fdd\u5b58\u8d77\u6765\u3002\u5f85\u5230\u51fd\u6570\u88ab\u771f\u6b63\u9700\u8981\u6c42\u503c\u7684\u65f6\u5019\uff0c\u4e4b\u524d\u4f20\u5165\u7684\u6240\u6709\u53c2\u6570\u90fd\u4f1a\u88ab\u4e00\u6b21\u6027\u7528\u4e8e\u6c42\u503c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u51fd\u6570\u67ef\u91cc\u5316\u5b9e\u9645\u5e94\u7528<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53c2\u6570\u590d\u7528<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c31\u662f\u5c06\u76f8\u540c\u7684\u53c2\u6570\u56fa\u5b9a\u4e0b\u6765\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6b63\u5e38\u6b63\u5219\u9a8c\u8bc1\u5b57\u7b26\u4e32 reg.test(txt)<br>\u200b<br>\/\/ \u51fd\u6570\u5c01\u88c5\u540e<br>function check(reg, txt) {<br> &nbsp; &nbsp;return reg.test(txt)<br>}<br>\u200b<br>\/\/ \u5373\u4f7f\u662f\u76f8\u540c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4e5f\u9700\u8981\u91cd\u65b0\u4f20\u9012\u4e00\u6b21<br>console.log(check(\/\\d+\/g, 'test1')); \/\/ true<br>console.log(check(\/\\d+\/g, 'testtest')); \/\/ false<br>console.log(check(\/&#91;a-z]+\/g, 'test')); \/\/ true<br>\u200b<br>\/\/ Currying\u540e<br>function curryingCheck(reg) {<br> &nbsp; &nbsp;return function (txt) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return reg.test(txt)<br> &nbsp;  }<br>}<br>\u200b<br>\/\/ \u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u8fc7\u95ed\u5305\u4fdd\u5b58\u4e86\u8d77\u6765<br>var hasNumber = curryingCheck(\/\\d+\/g)<br>var hasLetter = curryingCheck(\/&#91;a-z]+\/g)<br>\u200b<br>console.log(hasNumber('test1')); \/\/ true<br>console.log(hasNumber('testtest')); &nbsp;\/\/ false<br>console.log(hasLetter('21212')); \/\/ false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u793a\u4f8b\u662f\u4e00\u4e2a\u6b63\u5219\u7684\u6821\u9a8c\uff0c\u6b63\u5e38\u6765\u8bf4\u76f4\u63a5\u8c03\u7528 <em>check<\/em> \u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f46\u662f\u5982\u679c\u6211\u6709\u5f88\u591a\u5730\u65b9\u90fd\u8981\u6821\u9a8c\u662f\u5426\u6709\u6570\u5b57\uff0c\u5176\u5b9e\u5c31\u662f\u9700\u8981\u5c06\u7b2c\u4e00\u4e2a\u53c2\u6570 <em>reg<\/em> \u8fdb\u884c\u590d\u7528\uff0c\u8fd9\u6837\u522b\u7684\u5730\u65b9\u5c31\u80fd\u591f\u76f4\u63a5\u8c03\u7528 <em>hasNumber\u3001hasLetter<\/em> \u7b49\u51fd\u6570\uff0c\u8ba9\u53c2\u6570\u80fd\u591f\u590d\u7528\uff0c\u8c03\u7528\u8d77\u6765\u4e5f\u66f4\u65b9\u4fbf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c01\u88c5\u901a\u7528\u67ef\u91cc\u5316\u51fd\u6570<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u5c01\u88c5\u4e00\u4e2a\u901a\u7528\u7684\u67ef\u91cc\u5316\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function curry() {<br> &nbsp; &nbsp;var fn = arguments&#91;0]; \/\/ \u83b7\u53d6\u8981\u6267\u884c\u7684\u51fd\u6570<br> &nbsp; &nbsp;var args = &#91;].slice.call(arguments, 1); \/\/ \u83b7\u53d6\u4f20\u9012\u7684\u53c2\u6570\uff0c\u6784\u6210\u4e00\u4e2a\u53c2\u6570\u6570\u7ec4<br> &nbsp; &nbsp;\/\/ \u5982\u679c\u4f20\u9012\u7684\u53c2\u6570\u5df2\u7ecf\u7b49\u4e8e\u6267\u884c\u51fd\u6570\u6240\u9700\u7684\u53c2\u6570\u6570\u91cf<br> &nbsp; &nbsp;if (args.length === fn.length) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return fn.apply(this, args)<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/ \u53c2\u6570\u4e0d\u591f\u5411\u5916\u754c\u8fd4\u56de\u7684\u51fd\u6570<br> &nbsp; &nbsp;function _curry(){<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u63a8\u5165\u4e4b\u524d\u5224\u65ad<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5c06\u65b0\u63a5\u6536\u5230\u7684\u53c2\u6570\u63a8\u5165\u5230\u53c2\u6570\u6570\u7ec4\u4e2d<br> &nbsp; &nbsp; &nbsp; &nbsp;args.push(...arguments);<br> &nbsp; &nbsp; &nbsp; &nbsp;if(args.length === fn.length){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return fn.apply(this, args)<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;return _curry;<br> &nbsp;  }<br> &nbsp; &nbsp;return _curry;<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6d4b\u8bd5 1<br>function add(a, b, c) {<br> &nbsp; &nbsp;return a + b + c;<br>}<br>\u200b<br>console.log(curry(add)(1)(2)(3)); \/\/ 6<br>console.log(curry(add, 1)(2)(3)); \/\/ 6<br>console.log(curry(add, 1, 2, 3)); \/\/ 6<br>console.log(curry(add, 1)(3, 4)); \/\/ 8<br>\u200b<br>var addCurrying = curry(add)(2);<br>console.log(addCurrying(7)(8)); \/\/ 17<br>\u200b<br>\/\/ \u6d4b\u8bd5 2<br>function check(reg, txt) {<br> &nbsp; &nbsp;return reg.test(txt)<br>}<br>var hasNumber = curry(check)(\/\\d+\/g);<br>console.log(hasNumber('test1'));\/\/ true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u67ef\u91cc\u5316\uff08\u82f1\u8bed\uff1aCurrying\uff09\uff0c..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,11],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-js","category-front"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/js\/\" rel=\"category tag\">JS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a>","tag_info":"\u524d\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":226,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/223\/revisions\/226"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}