{"id":3439,"date":"2024-11-10T12:56:21","date_gmt":"2024-11-10T04:56:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3439\/"},"modified":"2024-11-10T12:56:21","modified_gmt":"2024-11-10T04:56:21","slug":"js%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%bf%e9%97%aejs%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3439\/","title":{"rendered":"js\u4e2d\u5982\u4f55\u8bbf\u95eejs\u6587\u4ef6"},"content":{"rendered":"<blockquote><p>\n  \u5728 js \u6587\u4ef6\u4e2d\u8bbf\u95ee\u5176\u4ed6 js \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 \u6807\u7b7e\u52a0\u8f7d\u5916\u90e8 js \u6587\u4ef6\u3002\u4f7f\u7528 es6 \u5bfc\u5165\u8bed\u53e5\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u4ee3\u7801\u3002\u4f7f\u7528\u52a8\u6001\u52a0\u8f7d\u5728\u8fd0\u884c\u65f6\u52a0\u8f7d\u5916\u90e8 js \u6587\u4ef6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812193659377.jpg\" class=\"aligncenter\" title=\"js\u4e2d\u5982\u4f55\u8bbf\u95eejs\u6587\u4ef6\u63d2\u56fe\" alt=\"js\u4e2d\u5982\u4f55\u8bbf\u95eejs\u6587\u4ef6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JS \u6587\u4ef6\u4e2d\u8bbf\u95ee JS \u4ee3\u7801<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8bbf\u95ee\u5176\u4ed6 JS \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 &lt;script&gt; \u6807\u7b7e&lt;\/script&gt;<\/strong><\/p>\n<p>\u8fd9\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4e5f\u662f\u6700\u5e38\u7528\u4e8e\u5f15\u5165\u5916\u90e8 JS \u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u5728 HTML \u6587\u6863\u4e2d\uff0c\u4f7f\u7528 &lt;script&gt; \u6807\u7b7e\uff0c\u5982\u4e0b\u6240\u793a\uff1a&lt;\/script&gt;<\/p>\n<pre>&lt;script src=\"path\/to\/script.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4f1a\u5c06 script.js \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u52a0\u8f7d\u5230\u5f53\u524d\u6587\u6863\u4e2d\u3002<\/p>\n<p><strong>2. \u4f7f\u7528\u5bfc\u5165\u8bed\u53e5<\/strong><\/p>\n<p>ES6 \u5f15\u5165\u4e86\u5bfc\u5165\u8bed\u53e5\uff0c\u5141\u8bb8\u76f4\u63a5\u4ece\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u4ee3\u7801\u3002\u5728 JS \u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528 import \u8bed\u53e5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>import {functionName, variableName} from 'path\/to\/module.js';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4f1a\u4ece module.js \u6587\u4ef6\u4e2d\u5bfc\u5165 functionName \u51fd\u6570\u548c variableName \u53d8\u91cf\u3002<\/p>\n<p><strong>3. \u4f7f\u7528\u52a8\u6001\u52a0\u8f7d<\/strong><\/p>\n<p>\u52a8\u6001\u52a0\u8f7d\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u52a0\u8f7d\u5916\u90e8 JS \u6587\u4ef6\u3002\u8fd9\u662f\u901a\u8fc7\u4f7f\u7528 XMLHttpRequest \u6216 fetch API \u6765\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528 XMLHttpRequest\nvar xhr = new XMLHttpRequest();\nxhr.open('GET', 'path\/to\/script.js');\nxhr.onload = function() {\n  eval(xhr.responseText);\n};\nxhr.send();\n\n\/\/ \u4f7f\u7528 fetch API\nfetch('path\/to\/script.js')\n  .then(response =&gt; response.text())\n  .then(script =&gt; eval(script));<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u8f83\u5c0f\u7684\u811a\u672c\uff0c&lt;script&gt; \u6807\u7b7e\u662f\u5feb\u901f\u800c\u7b80\u5355\u7684\u9009\u62e9\u3002&lt;\/script&gt; <\/li>\n<li>\u5bf9\u4e8e\u8f83\u5927\u7684\u811a\u672c\u6216\u9700\u8981\u6a21\u5757\u5316\u7684\u4ee3\u7801\uff0cimport \u8bed\u53e5\u66f4\u9002\u5408\u3002<\/li>\n<li>\u5728\u9700\u8981\u52a8\u6001\u52a0\u8f7d\u811a\u672c\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u52a8\u6001\u52a0\u8f7d\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u4e2d\u5982\u4f55\u8bbf\u95eejs\u6587\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 js \u6587\u4ef6\u4e2d\u8bbf\u95ee\u5176\u4ed6 js \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 \u6807\u7b7e\u52a0\u8f7d\u5916\u90e8 js \u6587\u4ef6\u3002\u4f7f\u7528 es6 \u5bfc\u5165\u8bed\u53e5\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u4ee3\u7801\u3002\u4f7f\u7528\u52a8\u6001\u52a0\u8f7d\u5728\u8fd0\u884c\u65f6\u52a0\u8f7d\u5916\u90e8 js \u6587\u4ef6\u3002 \u5982\u4f55\u5728 JS \u6587\u4ef6\u4e2d\u8bbf\u95ee JS \u4ee3\u7801 \u5728 JavaScript \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8bbf\u95ee\u5176\u4ed6 JS \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff1a 1. \u4f7f\u7528 &lt;script&gt; \u6807\u7b7e&lt;\/script&gt; \u8fd9\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4e5f\u662f\u6700\u5e38\u7528\u4e8e\u5f15\u5165\u5916\u90e8 JS \u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u5728 HTML \u6587\u6863\u4e2d\uff0c\u4f7f\u7528 &lt;script&gt; \u6807\u7b7e\uff0c\u5982\u4e0b\u6240\u793a\uff1a&lt;\/script&gt; &lt;script src=&#8221;path\/to\/script.js&#8221;&gt;&lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4f1a\u5c06 script.js \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u52a0\u8f7d\u5230\u5f53\u524d\u6587\u6863\u4e2d\u3002 2. \u4f7f\u7528\u5bfc\u5165\u8bed\u53e5 ES6 \u5f15\u5165\u4e86\u5bfc\u5165\u8bed\u53e5\uff0c\u5141\u8bb8\u76f4\u63a5\u4ece\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u4ee3\u7801\u3002\u5728 JS \u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528 import \u8bed\u53e5\uff0c\u5982\u4e0b\u6240\u793a\uff1a import {functionName, variableName} from &#8216;path\/to\/module.js&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4f1a\u4ece module.js [&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-3439","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3439","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=3439"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3439\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}