{"id":1155,"date":"2022-05-16T10:31:28","date_gmt":"2022-05-16T02:31:28","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1155"},"modified":"2022-05-16T10:54:18","modified_gmt":"2022-05-16T02:54:18","slug":"dan-li-mo-shi","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1155","title":{"rendered":"\u5355\u4f8b\u6a21\u5f0f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/\u9ed8\u8ba4\u6807\u9898_\u516c\u4f17\u53f7\u5c01\u9762\u9996\u56fe_2022-05-1417_30_05.png\" alt=\"\u9ed8\u8ba4\u6807\u9898_\u516c\u4f17\u53f7\u5c01\u9762\u9996\u56fe_2022-05-14+17_30_05\" \/><\/p>\n<p>\u5b9a\u4e49\uff1a<\/p>\n<blockquote>\n<p>\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u4ec5\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u8bbf\u95ee\u5b83\u7684\u5168\u5c40\u8bbf\u95ee\u70b9\u3002<\/p>\n<\/blockquote>\n<h2><a id=\"%E5%AE%9E%E7%8E%B0%E5%8D%95%E4%BE%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5b9e\u73b0\u5355\u4f8b<\/h2>\n<p>\u5982\u4f55\u5b9e\u73b0\u5355\u4f8b\uff1f<\/p>\n<blockquote>\n<p>\u65e0\u975e\u662f\u7528\u4e00\u4e2a\u53d8\u91cf\u6765\u6807\u8bc6\u5f53\u524d\u662f\u5426\u5df2\u7ecf\u4e3a\u67d0\u4e2a\u7c7b\u521b\u5efa\u8fc7\u5bf9\u8c61\uff0c\u5982\u679c\u662f\uff0c\u5219\u5728\u4e0b\u4e00\u6b21\u83b7\u53d6\u8be5\u7c7b\u7684\u5b9e\u4f8b\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de\u4e4b\u524d\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/p>\n<\/blockquote>\n<pre><code class=\"language-plain_text\">class Singleton {\n  instance = null;\n  static getInstance() {\n    if (!this.instance) {\n      this.instance = new Singleton()\n    }\n    return this.instance\n  }\n  prototypeMethods() {\n    console.log('Mes')\n  }\n}\n<\/code><\/pre>\n<pre><code class=\"language-plain_text\">console.log(Singleton.getInstance() === Singleton.getInstance()) \/\/ true\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684  <code>instance<\/code>  \u5c5e\u6027\u5c31\u662f\u90a3\u4e2a\u5b9e\u4f8b\u662f\u5426\u521b\u5efa\u7684\u6807\u8bc6\u3002\u5355\u4f8b\u7684\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let instance;\nif(!instance){\n    instance = xxx\n}\n<\/code><\/pre>\n<h3><a id=\"%E5%BA%94%E7%94%A8%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5e94\u7528\uff1a<\/h3>\n<ol>\n<li>\u767b\u5f55\u6846 \uff1a\u65e0\u8bba\u70b9\u51fb\u591a\u5c11\u6b21\uff0c\u5c31\u53ea\u6709\u4e00\u4e2a\u767b\u5f55\u5f39\u6846\uff1b<\/li>\n<li>Vuex\uff1a\u5355\u4e00\u6570\u636e\u6e90\uff1b<\/li>\n<\/ol>\n<p>\u767b\u5f55\u6846\uff1a<\/p>\n<pre><code class=\"language-plain_text\">class LoginForm {\n  instance = null;\n  static showLogin() {\n    if (this.instance) {\n      return this.instance;\n    }\n    this.createForm();\n    return (this.instance = this);\n  }\n  static createForm() {\n    console.log(&quot;create&quot;);\n    let form = document.createElement(&quot;div&quot;),\n      input = document.createElement(&quot;input&quot;);\n\n    input.placeholder = '\u8f93\u5165\u7528\u6237\u540d'\n    form.appendChild(input);\n    document.body.appendChild(form);\n  }\n}\n\nLoginForm.showLogin()\n<\/code><\/pre>\n<p>Vue \u4e2d\u5355\u4f8b\u7ec4\u4ef6\uff1a<\/p>\n<pre><code class=\"language-plain_text\">\n<\/code><\/pre>\n<h3><a id=\"%E6%89%A9%E5%B1%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u6269\u5c55<\/h3>\n<p>\u5982\u4f55\u628a\u672c\u6765\u4e0d\u662f\u4e00\u4e2a\u5355\u4f8b\u7684\u51fd\u6570\u5982\u4f55\u6539\u9020\u4e3a\u4e00\u4e2a\u5355\u4f8b\u5462\uff0c\u96be\u9053\u8981\u50cf\u4e0a\u9762\u6dfb\u52a0\u6807\u8bc6\u548c\u65b9\u6cd5\u5417\uff1f\u5982\u679c\u90fd\u8fd9\u6837\u505a\u7684\u8bdd\u6709\u70b9\u9ebb\u70e6\uff0c\u6211\u4eec\u53ef\u4ee5\u62bd\u8c61\u4e00\u4e2a\u5355\u4f8b\u5de5\u5382\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-plain_text\">\nfunction SingletonFactory(fn) {\n  if (!fn.instance) {\n    fn.instance = new (Function.prototype.bind.apply(fn, arguments));\n  }\n  return fn.instance\n}\n<\/code><\/pre>\n<p>\u6848\u4f8b\uff1a<\/p>\n<pre><code class=\"language-plain_text\">class Fun {\n  constructor(project, year) {\n    this.project = project;\n    this.year = year;\n  }\n  say() {\n    console.log(&quot;Fun say&quot;, this.project);\n  }\n}\nconsole.log(SingletonFactory(Fun, &quot;\u524d\u7aef\u9ed1\u677f\u62a5&quot;, 2017) == SingletonFactory(Fun, &quot;\u524d\u7aef\u9ed1\u677f\u62a5&quot;, 2017))\n<\/code><\/pre>\n<p>\u6ce8\uff1anew \u7684\u65f6\u5019\u4f20\u5165\u53c2\u6570\uff0c\u53c2\u8003 <a href=\"https:\/\/stackoverflow.com\/questions\/1606797\/use-of-apply-with-new-operator-is-this-possible\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/1606797\/use-of-apply-with-new-operator-is-this-possible<\/a> \u3002<\/p>\n<p>\u6216\u8005:<\/p>\n<pre><code class=\"language-plain_text\">let getSingle = function(fn){\n    let result;\n    return function(){\n        return result || (result = fn.apply(this,arguments))\n    }\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-plain_text\">function Test() {\n  console.log(1)\n  return this\n}\n\nlet getSingleTest = getSingle(Test)\n\nconsole.log(getSingleTest() == getSingleTest()) \/\/ 1 true\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528\u4ee3\u7406\u7c7b\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let ProxySingleton = (function () {\n  let instance\n  return function (fn) {\n    if (!instance) {\n      instance = new (Function.prototype.bind.apply(fn, arguments))();\n    }\n    return instance\n  }\n}())\n<\/code><\/pre>\n<p>\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-plain_text\">function Test(...args) {\n  \/\/console.log(args)\n}\n\nconsole.log(new ProxySingleton(Test,1,2) === new ProxySingleton(Test,1,2)) \/\/ true\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u4e49\uff1a \u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u4ec5\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u8bbf\u95ee\u5b83\u7684\u5168\u5c40\u8bbf\u95ee\u70b9\u3002 \u5b9e\u73b0\u5355\u4f8b \u5982\u4f55\u5b9e\u73b0\u5355\u4f8b\uff1f \u65e0\u975e\u662f\u7528\u4e00&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[6,123],"class_list":["post-1155","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-javascript","tag-123"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1155"}],"version-history":[{"count":3,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1155\/revisions"}],"predecessor-version":[{"id":1169,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1155\/revisions\/1169"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}