{"id":1497,"date":"2024-11-10T11:14:41","date_gmt":"2024-11-10T03:14:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1497\/"},"modified":"2024-11-10T11:14:41","modified_gmt":"2024-11-10T03:14:41","slug":"%e5%87%bd%e6%95%b0-javascript-%e6%8c%91%e6%88%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1497\/","title":{"rendered":"\u51fd\u6570 &#8211; JavaScript \u6311\u6218"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173044861949289.jpg\" class=\"aligncenter\" title=\"\u51fd\u6570 &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" alt=\"\u51fd\u6570 &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u5728 repo hub \u4e0a\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002<\/p>\n<hr>\n<h2> \u529f\u80fd\u76f8\u5173\u7684\u6311\u6218 <\/h2>\n<hr>\n<h3> \u53c2\u6570\u548c\u53c2\u6570 <\/h3>\n<pre>\/**\n * @param {function} fn\n * @return {number}\n *\/\n\nfunction functionlength(fn) {\n  return fn.length;\n}\n\n\/\/ usage example\nfunction myfunction(a, b, c) {\n  console.log(a, b, c);\n}\n\nconsole.log(functionlength(myfunction)); \/\/ =&gt; 3\n\n\/**\n * @param {...any} args\n * @return {number}\n *\/\n\nfunction numofarguments(...args) {\n  \/\/ return args.length;\n  return arguments.length;\n}\n\n\/\/ usage example\nconsole.log(numofarguments(1, 2, 3, 4, 5)); \/\/ =&gt; 5\nconsole.log(numofarguments()); \/\/ =&gt; 0\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u64b0\u5199 <\/h3>\n<pre>\/**\n * @param {...functions} fns\n * @return function\n *\/\n\nfunction compose(...fns) {\n  return function (x) {\n    let result = x;\n\n    for (const fn of fns.reverse()) {\n      result = fn(result);\n    }\n\n    return result;\n  };\n}\n\n\/\/ usage example\nconst add1 = (num) =&gt; num + 1;\nconst double = (num) =&gt; num * 2;\nconst subtract10 = (num) =&gt; num - 10;\n\nconst composedfn = compose(subtract10, double, add1);\nconsole.log(composedfn(3)); \/\/ (3 + 1) * 2 - 10 =&gt; -2\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u67ef\u91cc\u5316 <\/h3>\n<pre>\/**\n * @param {function} fn\n * @return {function}\n *\/\n\nfunction curry(fn) {\n  return function curried(...args) {\n    if (args.length &gt;= fn.length) {\n      return fn.apply(this, args);\n    }\n\n    return curried.bind(this, ...args);\n  };\n}\n\n\/\/ usage example\n\/\/ single parameter case\nfunction add(a, b) {\n  return a + b;\n}\n\nconst curriedadd = curry(add);\nconsole.log(curriedadd(3)(4)); \/\/ =&gt; 7\nconst alreadyaddedthree = curriedadd(3);\nconsole.log(alreadyaddedthree(4)); \/\/ =&gt; 7\n\n\/\/ fixed parameters case\nfunction addtwo(a, b) {\n  return a + b;\n}\n\nconst curriedaddtwo = curry(addtwo);\nconsole.log(curriedaddtwo(3, 4)); \/\/ =&gt; 7\nconsole.log(curriedaddtwo(3)(4)); \/\/ =&gt; 7\nconst alreadyaddedthreeb = curriedadd(3);\nconsole.log(alreadyaddedthreeb(4)); \/\/ =&gt; 7\n\n\/\/-------------------------------------------\n\n\/**\n * @param {function} fn\n * @return {function}\n *\/\n\nfunction curry(fn) {\n  return function curried(...args) {\n    const bindfn = curried.bind(this, ...args);\n    bindfn[symbol.toprimitive] = () =&gt; fn.call(this, ...args);\n\n    return bindfn;\n  };\n}\n\n\/\/ usage example\n\/\/ non-fixed parameters case\nfunction multiply(...numbers) {\n  return numbers.reduce((a, b) =&gt; a * b, 1);\n}\n\nconst curriedmultiply = curry(multiply);\nconst multiplybythree = curriedmultiply(3);\nconsole.log(multiplybythree); \/\/ =&gt; 3\nconsole.log(multiplybythree(4)); \/\/ =&gt; 12\n\nconst multiplybyfifteen = multiplybythree(5);\nconsole.log(multiplybyfifteen); \/\/ =&gt; 15\nconsole.log(multiplybyfifteen(2)); \/\/ =&gt; 30\n\nconsole.log(curriedmultiply(1)(2)(3)(4)); \/\/ =&gt; 24\nconsole.log(curriedmultiply(1, 2, 3, 4)); \/\/ =&gt; 24\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u5907\u5fd8\u5f55 <\/h3>\n<pre>\/**\n * @param {function} func\n * @return {function}\n *\/\n\nfunction memoize(fn) {\n  const cache = new map();\n\n  return function (arg) {\n    if (cache.has(arg)) {\n      return cache.get(arg);\n    }\n\n    const result = fn.call(this, arg);\n    cache.set(arg, result);\n\n    return result;\n  };\n}\n\n\/\/ usage example\nfunction expensivefunction(n) {\n  console.log(\"computing...\");\n  return n * 2;\n}\n\n\/\/ create a memoized version of the function.\nconst memoizedexpensivefunction = memoize(expensivefunction);\n\n\/\/ first call (computes and caches the result).\nconsole.log(memoizedexpensivefunction(5)); \/\/ =&gt; computing... 10\n\n\/\/ second call with the same argument (returns the cached result).\nconsole.log(memoizedexpensivefunction(5)); \/\/ =&gt; 10\n\n\/\/ third call with a different argument (computes and caches the new result).\nconsole.log(memoizedexpensivefunction(10)); \/\/ =&gt; computing... 20\n\n\/\/ fourth call with the same argument as the third call (returns the cached result).\nconsole.log(memoizedexpensivefunction(10)); \/\/ =&gt; 20\n\n\/\/ ----------------------------------------\n\/\/ when parameters could be array\n\/**\n * @param {function} fn\n * @return {function}\n *\/\n\nfunction memoize(fn) {\n  const cache = new map();\n\n  return function (...args) {\n    const key = json.stringify(args);\n\n    if (cache.has(key)) {\n      return cache.get(key);\n    }\n\n    const result = fn.call(this, ...args);\n    cache.set(key, result);\n\n    return result;\n  };\n}\n\n\/\/ usage example\nfunction expensivemul(a, b) {\n  console.log(\"computing...\");\n  return a * b;\n}\n\n\/\/ create a memoized version of the function.\nconst memoizedexpensivemul = memoize(expensivemul);\n\n\/\/ first call (computes and caches the result).\nconsole.log(memoizedexpensivemul(3, 7)); \/\/ =&gt; computing... 21\n\n\/\/ second call with the same argument (returns the cached result).\nconsole.log(memoizedexpensivemul(3, 7)); \/\/ =&gt; 21\n\n\/\/ third call with a different argument (computes and caches the new result).\nconsole.log(memoizedexpensivemul(5, 8)); \/\/ =&gt; computing... 40\n\n\/\/ fourth call with the same argument as the third call (returns the cached result).\nconsole.log(memoizedexpensivemul(5, 8)); \/\/ =&gt; 40\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u90e8\u5206\u7684 <\/h3>\n<pre>\/**\n * @param {Function} fn\n * @param {any[]} args\n * @returns {Function}\n *\/\n\nfunction partial(fn, ...args) {\n  return function (...restArgs) {\n    const copyArgs = args.map((arg) =&gt; {\n      return arg === partial.placeholder ? restArgs.shift() : arg;\n    });\n\n    return fn.call(this, ...copyArgs, ...restArgs);\n  };\n}\n\npartial.placeholder = Symbol();\n\n\/\/ Usage example\nconst func = (...args) =&gt; args;\nconst func123 = partial(func, 1, 2, 3);\nconsole.log(func123(4)); \/\/ =&gt; [1, 2, 3, 4]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h2> \u53c2\u8003 <\/h2>\n<ul>\n<li>\u4f1f\u5927\u7684\u524d\u7aef<\/li>\n<li>\u53c2\u6570\u5bf9\u8c61 &#8211; mdn<\/li>\n<li>\u53c2\u6570 &#8211; mdn<\/li>\n<li>\u51fd\u6570\u7ec4\u5408\uff08\u8ba1\u7b97\u673a\u79d1\u5b66\uff09- wikipedia.org<\/li>\n<li>11\u3002\u4ec0\u4e48\u662f\u6784\u56fe\uff1f\u521b\u5efa\u7ba1\u9053() &#8211; bfe.dev<\/li>\n<li>1.\u5b9e\u73b0 curry() &#8211; bfe.dev<\/li>\n<li>2.\u5b9e\u73b0\u5e26\u6709\u5360\u4f4d\u7b26\u652f\u6301\u7684 curry() &#8211; bfe.dev<\/li>\n<li>\u67ef\u91cc\u5316 &#8211; wikipedia.org<\/li>\n<li>14\u3002\u5b9e\u73b0\u901a\u7528\u8bb0\u5fc6\u529f\u80fd &#8211; memo() &#8211; bfe.dev<\/li>\n<li>122\u3002\u5b9e\u73b0 memoizeone() &#8211; bfe.dev<\/li>\n<li>\u8bb0\u5fc6 &#8211; wikipedia.org<\/li>\n<li>\u90e8\u5206\u5e94\u7528 &#8211; wikipedia.org<\/li>\n<li>139\u3002\u5b9e\u73b0 _.partial() &#8211; bfe.dev<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u51fd\u6570 &#8211; JavaScript \u6311\u6218\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u60a8\u53ef\u4ee5\u5728 repo hub \u4e0a\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002 \u529f\u80fd\u76f8\u5173\u7684\u6311\u6218 \u53c2\u6570\u548c\u53c2\u6570 \/** * @param {function} fn * @return {number} *\/ function functionlength(fn) { return fn.length; } \/\/ usage example function myfunction(a, b, c) { console.log(a, b, c); } console.log(functionlength(myfunction)); \/\/ =&gt; 3 \/** * @param {&#8230;any} args * @return {number} *\/ function numofarguments(&#8230;args) { \/\/ return args.length; return arguments.length; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1497","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1497"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1497\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}