{"id":3136,"date":"2024-11-10T13:55:16","date_gmt":"2024-11-10T05:55:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3136\/"},"modified":"2024-11-10T13:55:16","modified_gmt":"2024-11-10T05:55:16","slug":"js-%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3136\/","title":{"rendered":"js \u5982\u4f55\u8c03\u7528"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u5728 javascript \u4e2d\u8c03\u7528\u51fd\u6570\uff1f\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u901a\u8fc7\u53d8\u91cf\u8c03\u7528\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u65b9\u6cd5\u8c03\u7528\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u8c03\u7528\u51fd\u6570\u901a\u8fc7\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u8c03\u7528\u51fd\u6570\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110804421565132.jpg\" class=\"aligncenter\" title=\"js \u5982\u4f55\u8c03\u7528\u63d2\u56fe\" alt=\"js \u5982\u4f55\u8c03\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u8c03\u7528\u51fd\u6570<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u8c03\u7528\u51fd\u6570\uff1a<\/p>\n<p><strong>1. \u76f4\u63a5\u8c03\u7528<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u8c03\u7528\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>function greet() {\n  console.log(\"Hello!\");\n}\n\ngreet(); \/\/ \u8f93\u51fa \"Hello!\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u901a\u8fc7\u53d8\u91cf\u8c03\u7528<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u5c06\u51fd\u6570\u5206\u914d\u7ed9\u53d8\u91cf\uff0c\u7136\u540e\u901a\u8fc7\u8be5\u53d8\u91cf\u8c03\u7528\u5b83\uff1a<\/p>\n<pre>const sayHello = function () {\n  console.log(\"Hello!\");\n};\n\nsayHello(); \/\/ \u8f93\u51fa \"Hello!\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f5c\u4e3a\u65b9\u6cd5\u8c03\u7528<\/strong><\/p>\n<p>\u5982\u679c\u51fd\u6570\u662f\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u70b9\uff08.\uff09\u8fd0\u7b97\u7b26\u8c03\u7528\u5b83\uff1a<\/p>\n<pre>const person = {\n  name: \"John\",\n  greet() {\n    console.log(`Hello, my name is ${this.name}!`);\n  },\n};\n\nperson.greet(); \/\/ \u8f93\u51fa \"Hello, my name is John!\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u8c03\u7528<\/strong><\/p>\n<p>\u56de\u8c03\u51fd\u6570\u662f\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\u3002\u5f53\u53e6\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u56de\u8c03\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u88ab\u6267\u884c\uff1a<\/p>\n<pre>function doSomething(callback) {\n  callback();\n}\n\ndoSomething(function () {\n  console.log(\"Callback executed!\");\n}); \/\/ \u8f93\u51fa \"Callback executed!\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u901a\u8fc7\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u8c03\u7528<\/strong><\/p>\n<p>\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u662f\u9488\u5bf9\u7279\u5b9a\u4e8b\u4ef6\uff08\u5982\u5355\u51fb\u6216\u9f20\u6807\u60ac\u505c\uff09\u800c\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u5f53\u4e8b\u4ef6\u89e6\u53d1\u65f6\uff0c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5c06\u88ab\u8c03\u7528\uff1a<\/p>\n<pre>document.getElementById(\"button\").addEventListener(\"click\", function () {\n  console.log(\"Button clicked!\");\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e9b\u662f JavaScript \u4e2d\u8c03\u7528\u51fd\u6570\u6700\u5e38\u89c1\u7684\u4e94\u79cd\u65b9\u6cd5\u3002\u6b63\u786e\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u51fd\u6570\u7684\u5177\u4f53\u7528\u9014\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs \u5982\u4f55\u8c03\u7528\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>\u5982\u4f55\u5728 javascript \u4e2d\u8c03\u7528\u51fd\u6570\uff1f\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u901a\u8fc7\u53d8\u91cf\u8c03\u7528\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u65b9\u6cd5\u8c03\u7528\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u8c03\u7528\u51fd\u6570\u901a\u8fc7\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u8c03\u7528\u51fd\u6570 \u5982\u4f55\u5728 JavaScript \u4e2d\u8c03\u7528\u51fd\u6570 \u5728 JavaScript \u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u8c03\u7528\u51fd\u6570\uff1a 1. \u76f4\u63a5\u8c03\u7528 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u8c03\u7528\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a function greet() { console.log(&#8220;Hello!&#8221;); } greet(); \/\/ \u8f93\u51fa &#8220;Hello!&#8221; \u767b\u5f55\u540e\u590d\u5236 2. \u901a\u8fc7\u53d8\u91cf\u8c03\u7528 \u60a8\u53ef\u4ee5\u5c06\u51fd\u6570\u5206\u914d\u7ed9\u53d8\u91cf\uff0c\u7136\u540e\u901a\u8fc7\u8be5\u53d8\u91cf\u8c03\u7528\u5b83\uff1a const sayHello = function () { console.log(&#8220;Hello!&#8221;); }; sayHello(); \/\/ \u8f93\u51fa &#8220;Hello!&#8221; \u767b\u5f55\u540e\u590d\u5236 3. \u4f5c\u4e3a\u65b9\u6cd5\u8c03\u7528 \u5982\u679c\u51fd\u6570\u662f\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u70b9\uff08.\uff09\u8fd0\u7b97\u7b26\u8c03\u7528\u5b83\uff1a const person = { name: &#8220;John&#8221;, greet() { console.log(`Hello, my name is ${this.name}!`); }, [&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-3136","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3136","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=3136"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3136\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}