{"id":3214,"date":"2024-11-10T09:21:32","date_gmt":"2024-11-10T01:21:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3214\/"},"modified":"2024-11-10T09:21:32","modified_gmt":"2024-11-10T01:21:32","slug":"js%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%8f%a6%e4%b8%80%e4%b8%aajs","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3214\/","title":{"rendered":"js\u5982\u4f55\u8c03\u7528\u53e6\u4e00\u4e2ajs"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a javascript \u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u76f4\u63a5\u8c03\u7528\uff1a\u4f7f\u7528 \u6807\u7b7e\uff0c\u9002\u7528\u4e8e\u540c\u4e00\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u3002\u4f7f\u7528 ajax\uff1a\u9002\u7528\u4e8e\u8de8\u57df\u6587\u4ef6\uff0c\u901a\u8fc7 xmlhttprequest \u5bf9\u8c61\u8fdb\u884c\u5f02\u6b65\u8c03\u7528\u3002\u4f7f\u7528\u6a21\u5757\u52a0\u8f7d\u5668\uff1a\u4f7f\u7528 commonjs\u3001amd \u6216 es modules \u7b49\u6a21\u5757\u52a0\u8f7d\u5668\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110807184540139.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u8c03\u7528\u53e6\u4e00\u4e2ajs\u63d2\u56fe\" alt=\"js\u5982\u4f55\u8c03\u7528\u53e6\u4e00\u4e2ajs\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a JavaScript \u6587\u4ef6<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u8c03\u7528<\/strong><\/p>\n<p>\u5982\u679c\u53e6\u4e00\u4e2a JavaScript \u6587\u4ef6\u4f4d\u4e8e\u4e3b\u6587\u4ef6\u6240\u5728\u7684\u540c\u4e00\u76ee\u5f55\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 &lt;script&gt; \u6807\u7b7e\u76f4\u63a5\u8c03\u7528\uff1a&lt;\/script&gt;<\/p>\n<pre>&lt;script src=\"other-script.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 AJAX<\/strong><\/p>\n<p>\u5982\u679c\u53e6\u4e00\u4e2a JavaScript \u6587\u4ef6\u4f4d\u4e8e\u4e0d\u540c\u7684\u57df\u6216\u670d\u52a1\u5668\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528 AJAX\uff08\u5f02\u6b65 JavaScript \u548c XML\uff09\u6280\u672f\u8fdb\u884c\u8de8\u57df\u8c03\u7528\uff1a<\/p>\n<pre>var xhr = new XMLHttpRequest();\nxhr.open(\"GET\", \"https:\/\/example.com\/other-script.js\");\nxhr.onload = function() {\n  eval(xhr.responseText);\n};\nxhr.send();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528\u6a21\u5757\u52a0\u8f7d\u5668<\/strong><\/p>\n<p>\u73b0\u4ee3 JavaScript \u6846\u67b6\u901a\u5e38\u4f7f\u7528\u6a21\u5757\u52a0\u8f7d\u5668\u6765\u7ba1\u7406 JavaScript \u4ee3\u7801\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u5e38\u7528\u7684\u6a21\u5757\u52a0\u8f7d\u5668\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>CommonJS<\/li>\n<li>AMD<\/li>\n<li>ES Modules<\/li>\n<\/ul>\n<p><strong>CommonJS<\/strong><\/p>\n<pre>var otherScript = require(\".\/other-script.js\");\notherScript.init();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>AMD<\/strong><\/p>\n<pre>define([\"other-script\"], function(otherScript) {\n  otherScript.init();\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>ES Modules<\/strong><\/p>\n<pre>import { init } from \".\/other-script.js\";\ninit();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u53e6\u4e00\u4e2a JavaScript \u6587\u4ef6\u5df2\u52a0\u8f7d\u5230\u9875\u9762\u4e2d\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/li>\n<li>\u6ce8\u610f\u8de8\u57df\u9650\u5236\u3002\u5982\u679c\u53e6\u4e00\u4e2a JavaScript \u6587\u4ef6\u4f4d\u4e8e\u4e0d\u540c\u7684\u57df\uff0c\u5219\u53ef\u80fd\u9700\u8981\u914d\u7f6e CORS \u5934\u4ee5\u5141\u8bb8 AJAX \u8c03\u7528\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u6a21\u5757\u52a0\u8f7d\u5668\u53ef\u4ee5\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u6a21\u5757\u5316\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u8c03\u7528\u53e6\u4e00\u4e2ajs\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\u8c03\u7528\u53e6\u4e00\u4e2a javascript \u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u76f4\u63a5\u8c03\u7528\uff1a\u4f7f\u7528 \u6807\u7b7e\uff0c\u9002\u7528\u4e8e\u540c\u4e00\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u3002\u4f7f\u7528 ajax\uff1a\u9002\u7528\u4e8e\u8de8\u57df\u6587\u4ef6\uff0c\u901a\u8fc7 xmlhttprequest \u5bf9\u8c61\u8fdb\u884c\u5f02\u6b65\u8c03\u7528\u3002\u4f7f\u7528\u6a21\u5757\u52a0\u8f7d\u5668\uff1a\u4f7f\u7528 commonjs\u3001amd \u6216 es modules \u7b49\u6a21\u5757\u52a0\u8f7d\u5668\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a JavaScript \u6587\u4ef6 \u76f4\u63a5\u8c03\u7528 \u5982\u679c\u53e6\u4e00\u4e2a JavaScript \u6587\u4ef6\u4f4d\u4e8e\u4e3b\u6587\u4ef6\u6240\u5728\u7684\u540c\u4e00\u76ee\u5f55\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 &lt;script&gt; \u6807\u7b7e\u76f4\u63a5\u8c03\u7528\uff1a&lt;\/script&gt; &lt;script src=&#8221;other-script.js&#8221;&gt;&lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 AJAX \u5982\u679c\u53e6\u4e00\u4e2a JavaScript \u6587\u4ef6\u4f4d\u4e8e\u4e0d\u540c\u7684\u57df\u6216\u670d\u52a1\u5668\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528 AJAX\uff08\u5f02\u6b65 JavaScript \u548c XML\uff09\u6280\u672f\u8fdb\u884c\u8de8\u57df\u8c03\u7528\uff1a var xhr = new XMLHttpRequest(); xhr.open(&#8220;GET&#8221;, &#8220;https:\/\/example.com\/other-script.js&#8221;); xhr.onload = function() { eval(xhr.responseText); }; xhr.send(); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u6a21\u5757\u52a0\u8f7d\u5668 \u73b0\u4ee3 JavaScript [&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-3214","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3214","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=3214"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3214\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}