{"id":547,"date":"2025-12-06T16:09:06","date_gmt":"2025-12-06T08:09:06","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=547"},"modified":"2025-12-06T16:09:06","modified_gmt":"2025-12-06T08:09:06","slug":"%e5%89%8d%e7%ab%afjavascript%e4%b8%ad%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e7%9a%84%e5%ae%9e%e7%8e%b0%e4%b8%8e%e5%ba%94%e7%94%a8%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/06\/%e5%89%8d%e7%ab%afjavascript%e4%b8%ad%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e7%9a%84%e5%ae%9e%e7%8e%b0%e4%b8%8e%e5%ba%94%e7%94%a8%e5%ae%9e%e4%be%8b\/","title":{"rendered":"\u524d\u7aef(JavaScript)\u4e2d\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u4e0e\u5e94\u7528\u5b9e\u4f8b"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u300a\u524d\u7aef\uff08JavaScript\uff09\u4e2d\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u4e0e\u5e94\u7528\u5b9e\u4f8b\u300b<\/strong>\uff082025 \u6700\u65b0\u7248\uff09\uff0c\u6db5\u76d6\u6838\u5fc3\u5b9e\u73b0\u65b9\u5f0f\u3001ESModule \u7279\u6027\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f\u548c\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u9879\u76ee\u3001\u5b66\u4e60\u6216\u5199\u6587\u7ae0\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 \u524d\u7aef\uff08JavaScript\uff09\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u5b9e\u73b0\u4e0e\u5e94\u7528\u5b9e\u4f8b\uff082025 \u6700\u5168\u6307\u5357\uff09<\/h1>\n\n\n\n<p>\u5355\u4f8b\u6a21\u5f0f\uff08Singleton\uff09\uff1a<br><strong>\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u5728\u5168\u5c40\u53ea\u80fd\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u4e14\u53ef\u4ee5\u5168\u5c40\u8bbf\u95ee\u3002<\/strong><\/p>\n\n\n\n<p>JavaScript \u4e2d\u5355\u4f8b\u6a21\u5f0f\u5c24\u5176\u5e38\u7528\uff0c\u56e0\u4e3a\u6d4f\u89c8\u5668\u73af\u5883\u672c\u8eab\u5c31\u662f\u5929\u7136\u5168\u5c40\u5171\u4eab\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e00\u3001\u4e3a\u4ec0\u4e48 JS \u4e2d\u9700\u8981\u5355\u4f8b\u6a21\u5f0f\uff1f<\/h1>\n\n\n\n<p>\u524d\u7aef\u5e38\u89c1\u9700\u8981\u4fdd\u8bc1\u201c\u5168\u5c40\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u201d\u7684\u573a\u666f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u5168\u5c40\u72b6\u6001\u7ba1\u7406<\/td><td>\u5982\u7528\u6237\u4fe1\u606f\u3001\u6743\u9650\u3001\u914d\u7f6e<\/td><\/tr><tr><td>\u5168\u5c40\u4e8b\u4ef6\u603b\u7ebf EventBus<\/td><td>\u907f\u514d\u591a\u4e2a\u5b9e\u4f8b\u5bfc\u81f4\u4e8b\u4ef6\u4e22\u5931<\/td><\/tr><tr><td>WebSocket \u8fde\u63a5<\/td><td>\u6574\u4e2a\u5e94\u7528\u53ea\u80fd\u6709\u4e00\u4e2a\u8fde\u63a5<\/td><\/tr><tr><td>\u5f39\u7a97\u7ec4\u4ef6\uff08Dialog \/ Modal\uff09<\/td><td>\u907f\u514d\u591a\u6b21\u6e32\u67d3<\/td><\/tr><tr><td>\u7f13\u5b58\u7ba1\u7406\u5668<\/td><td>\u9632\u6b62\u91cd\u590d\u8bf7\u6c42\u63a5\u53e3<\/td><\/tr><tr><td>\u914d\u7f6e\u4e2d\u5fc3 Config<\/td><td>\u5168\u5c40\u7edf\u4e00\u914d\u7f6e\u5165\u53e3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e8c\u3001\u6700\u5e38\u7528\u7684 5 \u79cd\u5355\u4f8b\u5b9e\u73b0\u65b9\u5f0f\uff08\u542b\u6700\u4f73\u5b9e\u8df5\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2460 <strong>\u57fa\u4e8e\u95ed\u5305\u7684\u5355\u4f8b\uff08\u7ecf\u5178\u5199\u6cd5\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst Singleton = (function () {\n  let instance;\n\n  function createInstance() {\n    return { name: &quot;singleton instance&quot; };\n  }\n\n  return {\n    getInstance() {\n      if (!instance) instance = createInstance();\n      return instance;\n    }\n  };\n})();\n\nconst a = Singleton.getInstance();\nconst b = Singleton.getInstance();\n\nconsole.log(a === b); \/\/ true\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u70b9\uff1a\u7b80\u5355\u3001\u72ec\u7acb\uff0c\u4e0d\u4f9d\u8d56\u5916\u90e8\u7ed3\u6784<br>\u7f3a\u70b9\uff1a\u65e0\u6cd5\u4e0e ESModule \u76f8\u7ed3\u5408<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2461 <strong>\u57fa\u4e8e\u7c7b + \u9759\u6001\u5c5e\u6027\u7684\u5355\u4f8b\u6a21\u5f0f\uff08\u4e3b\u6d41\u63a8\u8350\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass Store {\n  static instance = null;\n\n  constructor() {\n    if (Store.instance) return Store.instance;\n    this.data = {};\n    Store.instance = this;\n  }\n}\n\nconst s1 = new Store();\nconst s2 = new Store();\n\nconsole.log(s1 === s2); \/\/ true\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ES6 \u9762\u5411\u5bf9\u8c61\u5199\u6cd5<\/li>\n\n\n\n<li>\u5b9e\u4f8b\u5929\u7136\u53ef\u6269\u5c55<\/li>\n\n\n\n<li>\u66f4\u9002\u5408\u5927\u578b\u9879\u76ee<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2462 <strong>\u4f7f\u7528 ESModule \u7684\u5929\u7136\u5355\u4f8b\uff08\u6700\u63a8\u8350\uff09<\/strong><\/h1>\n\n\n\n<p><strong>ESModule \u5728\u6d4f\u89c8\u5668 \/ Node \u4e2d\u5929\u7136\u53ea\u4f1a\u52a0\u8f7d\u4e00\u6b21\u3002<\/strong><\/p>\n\n\n\n<p><strong>\u26a1 \u4e00\u4e2a\u6a21\u5757\u5c31\u662f\u4e00\u4e2a\u5355\u4f8b\uff01<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ store.js\nexport const store = {\n  user: null,\n  token: null\n};\n\n\/\/ app.js\nimport { store } from &quot;.\/store.js&quot;;\nstore.user = { name: &quot;\u963f\u6770&quot; };\n\n<\/pre><\/div>\n\n\n<p>\u5728\u5176\u4ed6\u7ec4\u4ef6\u4e2d\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport { store } from &quot;.\/store.js&quot;;\nconsole.log(store.user); \/\/ \u6c38\u8fdc\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u70b9\uff1a<br>\u2714 \u6700\u5e72\u51c0<br>\u2714 \u65e0\u9700\u989d\u5916\u4ee3\u7801\u4fdd\u8bc1\u5355\u4f8b<br>\u2714 \u5929\u7136\u7f13\u5b58\uff08\u53ea\u6267\u884c\u4e00\u6b21\uff09<br>\u2714 Vue \/ React \u9879\u76ee\u901a\u7528<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u73b0\u4ee3\u524d\u7aef\u4f18\u5148\u63a8\u8350\uff1a\u4f7f\u7528 ESModule \u5b9e\u73b0\u5355\u4f8b\uff01<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2463 <strong>\u4f7f\u7528 Symbol + \u5168\u5c40\u6ce8\u518c\u8868\uff08\u89e3\u51b3\u591a\u7248\u672c\u51b2\u7a81\uff09<\/strong><\/h1>\n\n\n\n<p>\u7528\u4e8e\u907f\u514d\u591a\u4e2a bundle \u6ce8\u5165\u4e0d\u540c\u7248\u672c\u7684\u5b9e\u4f8b\uff0c\u5982\u5fae\u524d\u7aef\u573a\u666f\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst KEY = Symbol.for(&quot;global.singleton&quot;);\n\nif (!globalThis&#x5B;KEY]) {\n  globalThis&#x5B;KEY] = { count: 0 };\n}\n\nexport default globalThis&#x5B;KEY];\n\n<\/pre><\/div>\n\n\n<p>\u4efb\u4f55\u5730\u65b9\u5bfc\u5165\u90fd\u83b7\u5f97\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2464 <strong>\u7acb\u5373\u6267\u884c\u51fd\u6570 + WeakMap\uff08\u79c1\u6709\u5316\u5355\u4f8b\uff09<\/strong><\/h1>\n\n\n\n<p>\u7528\u4e8e\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282\uff0c\u4e0d\u80fd\u88ab\u5916\u90e8\u968f\u610f\u4fee\u6539\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst Singleton = (function () {\n  const privateMap = new WeakMap();\n\n  return class {\n    constructor() {\n      if (!privateMap.has(Singleton)) {\n        privateMap.set(Singleton, this);\n      }\n      return privateMap.get(Singleton);\n    }\n  };\n})();\n\nconst s1 = new Singleton();\nconst s2 = new Singleton();\n\nconsole.log(s1 === s2); \/\/ true\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e09\u3001\u524d\u7aef\u5b9e\u9645\u5e94\u7528\u573a\u666f\u793a\u4f8b\uff08\u8d85\u7ea7\u5b9e\u7528\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1. <strong>WebSocket \u5355\u4f8b\uff08\u6700\u5e38\u89c1\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass WS {\n  static instance = null;\n\n  constructor(url) {\n    if (WS.instance) return WS.instance;\n    this.ws = new WebSocket(url);\n    WS.instance = this;\n  }\n}\n\nexport default new WS(&quot;wss:\/\/server.com&quot;);\n\n<\/pre><\/div>\n\n\n<p>\u907f\u514d\u591a\u5f00 WebSocket \u8fde\u63a5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2. <strong>\u5168\u5c40\u4e8b\u4ef6\u603b\u7ebf\uff08EventBus\uff09\u5355\u4f8b<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass EventBus {\n  static instance;\n\n  constructor() {\n    if (EventBus.instance) return EventBus.instance;\n    this.events = {};\n    EventBus.instance = this;\n  }\n\n  on(name, fn) {\n    (this.events&#x5B;name] ||= &#x5B;]).push(fn);\n  }\n\n  emit(name, ...args) {\n    (this.events&#x5B;name] || &#x5B;]).forEach(fn =&gt; fn(...args));\n  }\n}\n\nexport default new EventBus();\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3. <strong>\u8bf7\u6c42\u7f13\u5b58\u5668\uff08\u9632\u6b62\u91cd\u590d\u8bf7\u6c42\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass Cache {\n  static instance;\n  constructor() {\n    if (Cache.instance) return Cache.instance;\n    this.map = new Map();\n    Cache.instance = this;\n  }\n\n  async fetch(url) {\n    if (this.map.has(url)) return this.map.get(url);\n    const data = await fetch(url).then(r =&gt; r.json());\n    this.map.set(url, data);\n    return data;\n  }\n}\n\nexport const cache = new Cache();\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4. <strong>\u5168\u5c40\u914d\u7f6e\u4e2d\u5fc3 Config<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport const config = {\n  api: &quot;\/api&quot;,\n  version: &quot;1.0.0&quot;,\n};\n\n<\/pre><\/div>\n\n\n<p>ESM \u5929\u7136\u5355\u4f8b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5. <strong>UI \u5f39\u7a97\uff08Modal\uff09\u5355\u4f8b<\/strong><\/h1>\n\n\n\n<p>\u907f\u514d\u91cd\u590d\u6e32\u67d3\u591a\u4e2a\u5f39\u7a97\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass ModalManager {\n  static instance;\n\n  constructor() {\n    if (ModalManager.instance) return ModalManager.instance;\n    this.visible = false;\n    ModalManager.instance = this;\n  }\n\n  show() { this.visible = true; }\n  hide() { this.visible = false; }\n}\n\nexport default new ModalManager();\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u56db\u3001\u6700\u4f73\u5b9e\u8df5\uff082025 \u7248\u672c\uff09<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f7f\u7528\u573a\u666f<\/th><th>\u63a8\u8350\u5355\u4f8b\u5b9e\u73b0\u65b9\u5f0f<\/th><\/tr><\/thead><tbody><tr><td>Vue\/React \u9879\u76ee<\/td><td><strong>ESModule \u5355\u4f8b\uff08\u6700\u5e72\u51c0\uff09<\/strong><\/td><\/tr><tr><td>\u5de5\u5177\u5e93 Class<\/td><td>\u7c7b\u9759\u6001\u5c5e\u6027\u5355\u4f8b<\/td><\/tr><tr><td>\u5fae\u524d\u7aef\u3001\u8de8\u5305\u5171\u4eab<\/td><td>Symbol.for + globalThis<\/td><\/tr><tr><td>\u9ad8\u6027\u80fd\u52a8\u753b\u3001\u6e38\u620f\u573a\u666f<\/td><td>IIFE + \u5bf9\u8c61\u6c60<\/td><\/tr><tr><td>\u79c1\u6709\u5316\u3001\u5b89\u5168\u6027\u8981\u6c42\u9ad8<\/td><td>WeakMap \u79c1\u6709\u5316<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e94\u3001\u603b\u7ed3\uff08\u53ef\u4f5c\u4e3a\u6587\u7ae0\u7ed3\u5c3e\uff09<\/h1>\n\n\n\n<p>JavaScript \u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u4f46 2025 \u5e74\u6700\u63a8\u8350\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u4f18\u5148\u4f7f\u7528 ESModule\uff0c\u56e0\u4e3a\u52a0\u8f7d\u4e00\u6b21\u3001\u5168\u5c40\u5171\u4eab\u3001\u6700\u7b26\u5408\u73b0\u4ee3\u524d\u7aef\u67b6\u6784\u3002<\/strong><br>\u5176\u4ed6\u65b9\u5f0f\u6839\u636e\u9700\u6c42\u8865\u5145\u4f7f\u7528\u5373\u53ef\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u300a\u524d\u7aef\uff08JavaScript\uff09\u4e2d\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u4e0e\u5e94\u7528\u5b9e\u4f8b\u300b\uff08202&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/06\/%e5%89%8d%e7%ab%afjavascript%e4%b8%ad%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e7%9a%84%e5%ae%9e%e7%8e%b0%e4%b8%8e%e5%ba%94%e7%94%a8%e5%ae%9e%e4%be%8b\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":548,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/547\/revisions\/548"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}