{"id":3183,"date":"2024-11-10T08:19:17","date_gmt":"2024-11-10T00:19:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3183\/"},"modified":"2024-11-10T08:19:17","modified_gmt":"2024-11-10T00:19:17","slug":"js%e5%a6%82%e4%bd%95%e5%86%99%e6%8f%92%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3183\/","title":{"rendered":"js\u5982\u4f55\u5199\u63d2\u4ef6"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u7f16\u5199\u63d2\u4ef6\u53ef\u6269\u5c55\u73b0\u6709\u529f\u80fd\uff0c\u65b9\u6cd5\u662f\uff1a1. \u521b\u5efa\u63d2\u4ef6\u6587\u4ef6\uff1b2. \u5b9a\u4e49\u63d2\u4ef6\u51fd\u6570\uff1b3. \u5b9a\u4e49\u63d2\u4ef6\u63a5\u53e3\uff1b4. \u5b9e\u73b0\u63d2\u4ef6\u903b\u8f91\uff1b5. \u5bfc\u51fa\u63d2\u4ef6\uff1b6. \u4f7f\u7528\u63d2\u4ef6\u3002\u4f8b\u5982\uff0c\u63d2\u4ef6\u53ef\u4ee5\u6dfb\u52a0\u8fb9\u6846\uff1amyplugin.addborder(element)\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110806280058413.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5199\u63d2\u4ef6\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5199\u63d2\u4ef6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u7f16\u5199\u63d2\u4ef6<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\u7f16\u5199\u63d2\u4ef6\u53ef\u4ee5\u6269\u5c55\u73b0\u6709\u5e94\u7528\u7a0b\u5e8f\u6216\u6846\u67b6\u7684\u529f\u80fd\uff0c\u5b9e\u73b0\u7279\u5b9a\u7684\u9700\u6c42\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u7f16\u5199 JavaScript \u63d2\u4ef6\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u63d2\u4ef6\u6587\u4ef6<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 JavaScript \u6587\u4ef6\uff0c\u4f5c\u4e3a\u63d2\u4ef6\u7684\u5165\u53e3\u70b9\uff0c\u5e76\u5c06\u5176\u547d\u540d\u4e3a\u63d2\u4ef6\u540d\u79f0\u3002\u4f8b\u5982\uff0c&#8217;my-plugin.js&#8217;\u3002<\/p>\n<p><strong>2. \u5b9a\u4e49\u63d2\u4ef6\u51fd\u6570<\/strong><\/p>\n<p>\u5728\u63d2\u4ef6\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5305\u542b\u63d2\u4ef6\u7684\u903b\u8f91\u3002\u51fd\u6570\u5e94\u8be5\u6709\u4e00\u4e2a\u540d\u79f0\uff0c\u4f5c\u4e3a\u63d2\u4ef6\u7684\u6807\u8bc6\u7b26\u3002<\/p>\n<p><strong>3. \u5b9a\u4e49\u63d2\u4ef6\u63a5\u53e3<\/strong><\/p>\n<p>\u63d2\u4ef6\u63a5\u53e3\u662f\u63d2\u4ef6\u4e0e\u5916\u90e8\u5e94\u7528\u7a0b\u5e8f\u6216\u6846\u67b6\u4ea4\u4e92\u7684\u65b9\u5f0f\u3002\u5b83\u53ef\u4ee5\u5305\u62ec\u516c\u5f00\u7684\u65b9\u6cd5\u3001\u5c5e\u6027\u6216\u4e8b\u4ef6\u3002\u901a\u5e38\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>(function(root, pluginName) {\n  root[pluginName] = {\n    \/\/ \u63d2\u4ef6\u63a5\u53e3\n  };\n})(window, 'myPlugin');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u5b9e\u73b0\u63d2\u4ef6\u903b\u8f91<\/strong><\/p>\n<p>\u5728\u63d2\u4ef6\u51fd\u6570\u4e2d\uff0c\u5b9e\u73b0\u63d2\u4ef6\u7684\u5b9e\u9645\u903b\u8f91\u3002\u8fd9\u53ef\u80fd\u5305\u62ec\u6570\u636e\u64cd\u4f5c\u3001\u4e8b\u4ef6\u5904\u7406\u6216\u5176\u4ed6\u529f\u80fd\u3002<\/p>\n<p><strong>5. \u5bfc\u51fa\u63d2\u4ef6<\/strong><\/p>\n<p>\u5c06\u63d2\u4ef6\u63a5\u53e3\u5bfc\u51fa\u5230\u5168\u5c40\u3002\u8fd9\u4f7f\u5e94\u7528\u7a0b\u5e8f\u6216\u6846\u67b6\u80fd\u591f\u8bbf\u95ee\u63d2\u4ef6\u3002\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>if (typeof define === 'function' &amp;&amp; define.amd) {\n  \/\/ AMD \u6a21\u5757\u7cfb\u7edf\n  define(['jquery'], function($) {\n    return myPlugin;\n  });\n} else if (typeof module === 'object' &amp;&amp; module.exports) {\n  \/\/ CommonJS \u6a21\u5757\u7cfb\u7edf\n  module.exports = myPlugin;\n} else {\n  \/\/ \u5168\u5c40\u53d8\u91cf\n  root[pluginName] = myPlugin;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u4f7f\u7528\u63d2\u4ef6<\/strong><\/p>\n<p>\u5728\u5e94\u7528\u7a0b\u5e8f\u6216\u6846\u67b6\u4e2d\uff0c\u901a\u8fc7\u5176\u540d\u79f0\u8bbf\u95ee\u63d2\u4ef6\u63a5\u53e3\u5e76\u4f7f\u7528\u5176\u65b9\u6cd5\u6216\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>myPlugin.doSomething();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684 JavaScript \u63d2\u4ef6\u793a\u4f8b\uff0c\u5b83\u5411\u5143\u7d20\u6dfb\u52a0\u4e00\u4e2a\u8fb9\u6846\uff1a<\/p>\n<pre>\/\/ my-plugin.js\n(function(root, pluginName) {\n  root[pluginName] = {\n    addBorder: function(element) {\n      element.style.border = '1px solid red';\n    }\n  };\n})(window, 'myPlugin');\n\n\/\/ index.js\nmyPlugin.addBorder(document.getElementById('myElement'));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u5728 JavaScript \u4e2d\u7f16\u5199\u63d2\u4ef6\u6709\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5199\u63d2\u4ef6\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>\u5728 javascript \u4e2d\u7f16\u5199\u63d2\u4ef6\u53ef\u6269\u5c55\u73b0\u6709\u529f\u80fd\uff0c\u65b9\u6cd5\u662f\uff1a1. \u521b\u5efa\u63d2\u4ef6\u6587\u4ef6\uff1b2. \u5b9a\u4e49\u63d2\u4ef6\u51fd\u6570\uff1b3. \u5b9a\u4e49\u63d2\u4ef6\u63a5\u53e3\uff1b4. \u5b9e\u73b0\u63d2\u4ef6\u903b\u8f91\uff1b5. \u5bfc\u51fa\u63d2\u4ef6\uff1b6. \u4f7f\u7528\u63d2\u4ef6\u3002\u4f8b\u5982\uff0c\u63d2\u4ef6\u53ef\u4ee5\u6dfb\u52a0\u8fb9\u6846\uff1amyplugin.addborder(element)\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u7f16\u5199\u63d2\u4ef6 \u5728 JavaScript \u4e2d\u7f16\u5199\u63d2\u4ef6\u53ef\u4ee5\u6269\u5c55\u73b0\u6709\u5e94\u7528\u7a0b\u5e8f\u6216\u6846\u67b6\u7684\u529f\u80fd\uff0c\u5b9e\u73b0\u7279\u5b9a\u7684\u9700\u6c42\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u7f16\u5199 JavaScript \u63d2\u4ef6\uff1a 1. \u521b\u5efa\u63d2\u4ef6\u6587\u4ef6 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 JavaScript \u6587\u4ef6\uff0c\u4f5c\u4e3a\u63d2\u4ef6\u7684\u5165\u53e3\u70b9\uff0c\u5e76\u5c06\u5176\u547d\u540d\u4e3a\u63d2\u4ef6\u540d\u79f0\u3002\u4f8b\u5982\uff0c&#8217;my-plugin.js&#8217;\u3002 2. \u5b9a\u4e49\u63d2\u4ef6\u51fd\u6570 \u5728\u63d2\u4ef6\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5305\u542b\u63d2\u4ef6\u7684\u903b\u8f91\u3002\u51fd\u6570\u5e94\u8be5\u6709\u4e00\u4e2a\u540d\u79f0\uff0c\u4f5c\u4e3a\u63d2\u4ef6\u7684\u6807\u8bc6\u7b26\u3002 3. \u5b9a\u4e49\u63d2\u4ef6\u63a5\u53e3 \u63d2\u4ef6\u63a5\u53e3\u662f\u63d2\u4ef6\u4e0e\u5916\u90e8\u5e94\u7528\u7a0b\u5e8f\u6216\u6846\u67b6\u4ea4\u4e92\u7684\u65b9\u5f0f\u3002\u5b83\u53ef\u4ee5\u5305\u62ec\u516c\u5f00\u7684\u65b9\u6cd5\u3001\u5c5e\u6027\u6216\u4e8b\u4ef6\u3002\u901a\u5e38\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a (function(root, pluginName) { root[pluginName] = { \/\/ \u63d2\u4ef6\u63a5\u53e3 }; })(window, &#8216;myPlugin&#8217;); \u767b\u5f55\u540e\u590d\u5236 4. \u5b9e\u73b0\u63d2\u4ef6\u903b\u8f91 \u5728\u63d2\u4ef6\u51fd\u6570\u4e2d\uff0c\u5b9e\u73b0\u63d2\u4ef6\u7684\u5b9e\u9645\u903b\u8f91\u3002\u8fd9\u53ef\u80fd\u5305\u62ec\u6570\u636e\u64cd\u4f5c\u3001\u4e8b\u4ef6\u5904\u7406\u6216\u5176\u4ed6\u529f\u80fd\u3002 5. \u5bfc\u51fa\u63d2\u4ef6 \u5c06\u63d2\u4ef6\u63a5\u53e3\u5bfc\u51fa\u5230\u5168\u5c40\u3002\u8fd9\u4f7f\u5e94\u7528\u7a0b\u5e8f\u6216\u6846\u67b6\u80fd\u591f\u8bbf\u95ee\u63d2\u4ef6\u3002\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a if (typeof define === &#8216;function&#8217; &amp;&amp; define.amd) { \/\/ [&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-3183","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3183","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=3183"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3183\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}