{"id":1543,"date":"2024-11-10T08:49:39","date_gmt":"2024-11-10T00:49:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1543\/"},"modified":"2024-11-10T08:49:39","modified_gmt":"2024-11-10T00:49:39","slug":"%e9%97%ad%e5%8c%85-javascript-%e6%8c%91%e6%88%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1543\/","title":{"rendered":"\u95ed\u5305 &#8211; JavaScript \u6311\u6218"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173046114318329.jpg\" class=\"aligncenter\" title=\"\u95ed\u5305 &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" alt=\"\u95ed\u5305 &#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> \u5173\u95ed\u76f8\u5173\u7684\u6311\u6218 <\/h2>\n<hr>\n<h3> \u4f60\u597d\u4e16\u754c <\/h3>\n<pre>\/**\n * @return {function}\n *\/\n\nfunction createhelloworld() {\n  return function (...args) {\n    return \"hello world\";\n  };\n}\n\n\/\/ usage example\nconst output = createhelloworld();\nconsole.log(output()); \/\/ =&gt; \"hello world\"\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u6dfb\u52a0 <\/h3>\n<pre>\/**\n * @param  {...any} args\n * @return {function | number}\n *\/\n\nfunction add(...args) {\n  let sum = args.reduce((acc, val) =&gt; acc + val, 0);\n\n  function inneradd(...moreargs) {\n    sum += moreargs.reduce((acc, val) =&gt; acc + val, 0);\n    return inneradd;\n  }\n  inneradd.getvalue = function () {\n    return sum;\n  };\n\n  return inneradd;\n}\n\n\/\/ usage example\nconsole.log(add(1).getvalue()); \/\/ =&gt; 1\nconsole.log(add(1)(2).getvalue()); \/\/ =&gt; 3\nconsole.log(add(1)(2)(3).getvalue()); \/\/ =&gt; 6\nconsole.log(add(1)(2, 3).getvalue()); \/\/ =&gt; 6\nconsole.log(add(1, 2)(3).getvalue()); \/\/ =&gt; 6\nconsole.log(add(1, 2, 3).getvalue()); \/\/ =&gt; 6\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u548c <\/h3>\n<pre>\/**\n * @param {number} num\n *\/\n\nfunction sum(num) {\n  const func = function (num2) {\n    return num2 ? sum(num + num2) : num;\n  };\n\n  func.valueof = () =&gt; num;\n  return func;\n}\n\n\/\/ usage example\nconst sum1 = sum(1);\nconsole.log(sum1(2) == 3); \/\/ =&gt; true\nconsole.log(sum1(3) == 4); \/\/ =&gt; true\nconsole.log(sum(1)(2)(3) == 6); \/\/ =&gt; true\nconsole.log(sum(5)(-1)(2) == 6); \/\/ =&gt; true\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u67dc\u53f0 <\/h3>\n<pre>\/**\n * @param {number} initialvalue\n * @return {function}\n *\/\n\nfunction makecounter(initialvalue = 0) {\n  let count = initialvalue - 1;\n\n  return function (...args) {\n    count += 1;\n    return count;\n  };\n}\n\n\/\/ usage example\nconst counter = makecounter(0);\nconsole.log(counter()); \/\/ =&gt; 0\nconsole.log(counter()); \/\/ =&gt; 1\nconsole.log(counter()); \/\/ =&gt; 2\n\n\/\/------------------------------\n\/\/ return an object\n\/**\n * @param {number} initialvalue\n * @return {{get: function, increment: function, decrement: function, reset: function }}\n *\/\nfunction makecounter(initialvalue = 0) {\n  let count = initialvalue;\n\n  return {\n    get: () =&gt; count,\n    increment: () =&gt; ++count,\n    decrement: () =&gt; --count,\n    reset: () =&gt; (count = initialvalue),\n  };\n}\n\n\/\/ usage example\nconst counterobj = makecounter(0);\nconsole.log(counterobj.get()); \/\/ =&gt; 0\ncounterobj.increment();\nconsole.log(counterobj.get()); \/\/ =&gt; 1\ncounterobj.decrement();\ncounterobj.reset();\nconsole.log(counterobj.get()); \/\/ =&gt; 0\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u5faa\u73af <\/h3>\n<pre>\/**\n * @template t\n * @param  {...t} values\n * @returns () =&gt; t\n *\/\n\nfunction cycle(...values) {\n  let index = -1;\n\n  return function (...args) {\n    index = (index + 1) % values.length;\n    return values[index];\n  };\n}\n\n\/\/ usage example\nconst hellofn = cycle(\"hello\");\nconsole.log(hellofn()); \/\/ =&gt; \"hello\"\nconsole.log(hellofn()); \/\/ =&gt; \"hello\"\n\nconst onofffn = cycle(\"on\", \"off\");\nconsole.log(onofffn()); \/\/ =&gt; \"on\"\nconsole.log(onofffn()); \/\/ =&gt; \"off\"\nconsole.log(onofffn()); \/\/ =&gt; \"on\"\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u9650\u5236 <\/h3>\n<pre>\/**\n * @param {function} func\n * @param {number} count\n * @return {function}\n *\/\n\nfunction limit(fn, max) {\n  let count = 0;\n  let value;\n\n  return function (...args) {\n    if (count &lt; max) {\n      value = fn.call(this, ...args);\n      count++;\n    }\n\n    return value;\n  };\n}\n\n\/\/ usage example\nlet i = 1;\nfunction incrementby(value) {\n  i += value;\n  return i;\n}\n\nconst incrementbyatmostthrice = limit(incrementby, 3);\nconsole.log(incrementbyatmostthrice(2)); \/\/ i is now 3; the function returns 3.\nconsole.log(incrementbyatmostthrice(3)); \/\/ i is now 6; the function returns 6.\nconsole.log(incrementbyatmostthrice(4)); \/\/ i is now 10; the function returns 10.\nconsole.log(incrementbyatmostthrice(5)); \/\/ i is still 10 as this is the 4th invocation; the function returns 10 as it's the result of the last invocation.\ni = 4;\nconsole.log(incrementbyatmostthrice(2)); \/\/ i is still 4 as it is not modified. the function still returns 10.\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u4e00\u6b21 <\/h3>\n<pre>\/**\n * @param {function} fn\n * @return {function}\n *\/\n\nfunction once(fn) {\n  let ranonce = false;\n  let value;\n\n  return function (...args) {\n    if (!ranonce) {\n      value = fn.call(this, ...args);\n      ranonce = true;\n    }\n\n    return value;\n  };\n}\n\n\/\/ usage example\nfunction func(num) {\n  return num;\n}\n\nconst onced = once(func);\nconsole.log(onced(1)); \/\/ =&gt; 1, func called with 1\nconsole.log(onced(2)); \/\/ =&gt; 1, even 2 is passed, previous result is returned\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u5b58\u5728\u6216\u4e0d\u5b58\u5728 <\/h3>\n<pre>\/**\n * @param {any} val\n * @return {true | Error}\n *\/\n\nfunction expect(val) {\n  return {\n    toBe: function (arg) {\n      if (val === arg) {\n        return true;\n      } else {\n        throw new Error(\"Not Equal\");\n      }\n    },\n    notToBe: function (arg) {\n      if (val !== arg) {\n        return true;\n      } else {\n        throw new Error(\"Equal\");\n      }\n    },\n  };\n}\n\n\/\/ Usage example\nexpect(5).toBe(5); \/\/ Passes\nexpect(5).notToBe(6); \/\/ Passes\n\ntry {\n  expect(5).toBe(6); \/\/ Throws an error\n} catch (error) {\n  console.log(error.message); \/\/ Not Equal\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h2> \u53c2\u8003 <\/h2>\n<ul>\n<li>148\u3002\u521b\u5efa\u4e00\u4e2a\u8ba1\u6570\u5668\u5bf9\u8c61 &#8211; bfe.dev<\/li>\n<li>2620\u3002\u8ba1\u6570\u5668 &#8211; leetcode<\/li>\n<li>2665\u3002\u8ba1\u6570\u5668 ii &#8211; leetcode<\/li>\n<li>2665\u3002\u8ba1\u6570\u5668 ii &#8211; leetcode<\/li>\n<li>\u4f1f\u5927\u7684\u524d\u7aef<\/li>\n<li>2667\u3002\u521b\u5efa hello world \u51fd\u6570 &#8211; leetcode<\/li>\n<li>23\u3002\u521b\u5efa sum() &#8211; bfe.dev<\/li>\n<li>46\u3002\u5b9e\u73b0 _.once() &#8211; bfe.dev<\/li>\n<li>2704\u3002\u751f\u5b58\u8fd8\u662f\u6bc1\u706d &#8211; bfe.dev<\/li>\n<li>161\u3002 tobe() \u6216 not.tobe() &#8211; bfe.dev<\/li>\n<li> \u201c\u4f60\u597d\u4e16\u754c\uff01\u201d\u7a0b\u5e8f &#8211; wikipedia.org<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u95ed\u5305 &#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 \u5173\u95ed\u76f8\u5173\u7684\u6311\u6218 \u4f60\u597d\u4e16\u754c \/** * @return {function} *\/ function createhelloworld() { return function (&#8230;args) { return &#8220;hello world&#8221;; }; } \/\/ usage example const output = createhelloworld(); console.log(output()); \/\/ =&gt; &#8220;hello world&#8221; \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0 \/** * @param {&#8230;any} args * @return {function | number} *\/ function add(&#8230;args) { let sum = args.reduce((acc, [&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-1543","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1543","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=1543"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1543\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}