{"id":3404,"date":"2024-11-10T16:51:07","date_gmt":"2024-11-10T08:51:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3404\/"},"modified":"2024-11-10T16:51:07","modified_gmt":"2024-11-10T08:51:07","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9js-%e8%b0%83%e7%94%a8js","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3404\/","title":{"rendered":"\u5982\u4f55\u8ba9js \u8c03\u7528js"},"content":{"rendered":"<blockquote><p>\n  javascript \u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8c03\u7528\u53e6\u4e00\u4e2a javascript\uff1a\u901a\u8fc7\u811a\u672c\u6807\u7b7e\u76f4\u63a5\u8c03\u7528\uff1a \u6807\u7b7e\u53ef\u76f4\u63a5\u5305\u542b\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u540d\u8fdb\u884c\u8c03\u7528\u3002\u901a\u8fc7\u5168\u5c40\u5bf9\u8c61\u95f4\u63a5\u8c03\u7528\uff1a\u5c06\u51fd\u6570\u6302\u8f7d\u5230\u5168\u5c40\u5bf9\u8c61\uff08\u5982 window\uff09\u6216\u81ea\u5b9a\u4e49\u5bf9\u8c61\u4e0a\u8fdb\u884c\u95f4\u63a5\u8c03\u7528\u3002\u901a\u8fc7\u56de\u8c03\u51fd\u6570\uff1a\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8c03\u7528\u3002\u901a\u8fc7\u4e8b\u4ef6\u76d1\u542c\u5668\uff1a\u4fa6\u542c\u7279\u5b9a\u4e8b\u4ef6\u5e76\u6267\u884c\u4ee3\u7801\u3002\u901a\u8fc7 ajax \u8bf7\u6c42\uff1a\u5f02\u6b65\u52a0\u8f7d javascript \u6587\u4ef6\u5e76\u52a8\u6001\u8c03\u7528\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110811400262312.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u8ba9js \u8c03\u7528js\u63d2\u56fe\" alt=\"\u5982\u4f55\u8ba9js \u8c03\u7528js\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8ba9 JavaScript \u8c03\u7528 JavaScript<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u8c03\u7528<\/strong><\/p>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 script \u6807\u7b7e\uff1a<\/p>\n<pre>&lt;script&gt;\n  function myFunction1() {\n    alert(\"\u8c03\u7528\u4e86 myFunction1\");\n  }\n&lt;\/script&gt;\n&lt;script&gt;\n  function myFunction2() {\n    alert(\"\u8c03\u7528\u4e86 myFunction2\");\n  }\n  myFunction1(); \/\/ \u76f4\u63a5\u8c03\u7528 myFunction1\n&lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u95f4\u63a5\u8c03\u7528<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u6302\u8f7d\u5230\u5168\u5c40\u5bf9\u8c61\u7684\u65b9\u6cd5\u8fdb\u884c\u95f4\u63a5\u8c03\u7528\uff1a<\/p>\n<ul>\n<li><strong>\u6302\u8f7d\u5230 window \u5bf9\u8c61\uff1a<\/strong><\/li>\n<\/ul>\n<pre>window.myFunction = function() {\n  alert(\"\u8c03\u7528\u4e86 myFunction\");\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li><strong>\u6302\u8f7d\u5230\u5176\u4ed6\u5bf9\u8c61\uff1a<\/strong><\/li>\n<\/ul>\n<pre>const myObject = {\n  myFunction: function() {\n    alert(\"\u8c03\u7528\u4e86 myFunction\");\n  }\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u56de\u8c03\u51fd\u6570<\/strong><\/p>\n<p>\u56de\u8c03\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u9002\u5f53\u7684\u65f6\u5019\u88ab\u8c03\u7528\uff1a<\/p>\n<pre>function callMyFunction(callback) {\n  callback();\n}\ncallMyFunction(function() {\n  alert(\"\u8c03\u7528\u4e86\u56de\u8c03\u51fd\u6570\");\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8b\u4ef6\u76d1\u542c\u5668<\/strong><\/p>\n<p>\u4e8b\u4ef6\u76d1\u542c\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u56de\u8c03\u51fd\u6570\uff0c\u7528\u4e8e\u4fa6\u542c\u7279\u5b9a\u4e8b\u4ef6\u5e76\u6267\u884c\u4ee3\u7801\uff1a<\/p>\n<pre>document.addEventListener(\"click\", function() {\n  alert(\"\u70b9\u51fb\u4e86\u9875\u9762\");\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>AJAX \u8bf7\u6c42<\/strong><\/p>\n<p>AJAX \u8bf7\u6c42\u53ef\u4ee5\u7528\u6765\u548c\u5904\u7406 JavaScript \u6587\u4ef6\uff0c\u4ece\u800c\u5b9e\u73b0 JavaScript \u7684\u52a8\u6001\u8c03\u7528\uff1a<\/p>\n<pre>const xhr = new XMLHttpRequest();\nxhr.open(\"GET\", \"myScript.js\");\nxhr.onload = function() {\n  eval(xhr.responseText); \/\/ \u6267\u884c\u52a0\u8f7d\u7684 JavaScript \u4ee3\u7801\n};\nxhr.send();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u8ba9js \u8c03\u7528js\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>javascript \u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8c03\u7528\u53e6\u4e00\u4e2a javascript\uff1a\u901a\u8fc7\u811a\u672c\u6807\u7b7e\u76f4\u63a5\u8c03\u7528\uff1a \u6807\u7b7e\u53ef\u76f4\u63a5\u5305\u542b\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u540d\u8fdb\u884c\u8c03\u7528\u3002\u901a\u8fc7\u5168\u5c40\u5bf9\u8c61\u95f4\u63a5\u8c03\u7528\uff1a\u5c06\u51fd\u6570\u6302\u8f7d\u5230\u5168\u5c40\u5bf9\u8c61\uff08\u5982 window\uff09\u6216\u81ea\u5b9a\u4e49\u5bf9\u8c61\u4e0a\u8fdb\u884c\u95f4\u63a5\u8c03\u7528\u3002\u901a\u8fc7\u56de\u8c03\u51fd\u6570\uff1a\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8c03\u7528\u3002\u901a\u8fc7\u4e8b\u4ef6\u76d1\u542c\u5668\uff1a\u4fa6\u542c\u7279\u5b9a\u4e8b\u4ef6\u5e76\u6267\u884c\u4ee3\u7801\u3002\u901a\u8fc7 ajax \u8bf7\u6c42\uff1a\u5f02\u6b65\u52a0\u8f7d javascript \u6587\u4ef6\u5e76\u52a8\u6001\u8c03\u7528\u3002 \u5982\u4f55\u8ba9 JavaScript \u8c03\u7528 JavaScript \u76f4\u63a5\u8c03\u7528 \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 script \u6807\u7b7e\uff1a &lt;script&gt; function myFunction1() { alert(&#8220;\u8c03\u7528\u4e86 myFunction1&#8221;); } &lt;\/script&gt; &lt;script&gt; function myFunction2() { alert(&#8220;\u8c03\u7528\u4e86 myFunction2&#8221;); } myFunction1(); \/\/ \u76f4\u63a5\u8c03\u7528 myFunction1 &lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 \u95f4\u63a5\u8c03\u7528 \u53ef\u4ee5\u901a\u8fc7\u6302\u8f7d\u5230\u5168\u5c40\u5bf9\u8c61\u7684\u65b9\u6cd5\u8fdb\u884c\u95f4\u63a5\u8c03\u7528\uff1a \u6302\u8f7d\u5230 window \u5bf9\u8c61\uff1a window.myFunction = function() { alert(&#8220;\u8c03\u7528\u4e86 myFunction&#8221;); }; \u767b\u5f55\u540e\u590d\u5236 \u6302\u8f7d\u5230\u5176\u4ed6\u5bf9\u8c61\uff1a const myObject = [&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-3404","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3404","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=3404"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3404\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}