{"id":3219,"date":"2024-11-10T11:46:51","date_gmt":"2024-11-10T03:46:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3219\/"},"modified":"2024-11-10T11:46:51","modified_gmt":"2024-11-10T03:46:51","slug":"%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8js%e9%87%8c%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3219\/","title":{"rendered":"\u5982\u4f55\u8c03\u7528js\u91cc\u7684\u65b9\u6cd5"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\uff0c\u8c03\u7528\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\uff1a\u76f4\u63a5\u8c03\u7528\u5168\u5c40\u65b9\u6cd5\u6216\u53d8\u91cf\u3002\u4f7f\u7528\u70b9\u53f7\u7b26\u53f7\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110807275393340.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u8c03\u7528js\u91cc\u7684\u65b9\u6cd5\u63d2\u56fe\" alt=\"\u5982\u4f55\u8c03\u7528js\u91cc\u7684\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8c03\u7528 JavaScript \u91cc\u9762\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u8c03\u7528 JavaScript \u91cc\u9762\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<p><strong>1. \u76f4\u63a5\u8c03\u7528<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\u5168\u5c40\u65b9\u6cd5\u6216\u8005\u53d8\u91cf\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\u7684\u540d\u79f0\u8fdb\u884c\u8c03\u7528\uff0c\u65e0\u9700\u4f7f\u7528\u70b9\u53f7\u7b26\u53f7\u6216\u5176\u4ed6\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>console.log(\"Hello, world!\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u4f7f\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5f53\u65b9\u6cd5\u5c5e\u4e8e\u67d0\u4e2a\u5bf9\u8c61\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u70b9\u53f7\u7b26\u53f7\u5c06\u65b9\u6cd5\u540d\u79f0\u9644\u52a0\u5230\u5bf9\u8c61\u540d\u79f0\u540e\u9762\u8fdb\u884c\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a myObject \u7684\u5bf9\u8c61\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a sayHello \u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>myObject.sayHello();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0b\u9762\u662f JavaScript \u4e2d\u8c03\u7528\u65b9\u6cd5\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u627e\u5230\u65b9\u6cd5\u7684\u5b9a\u4e49\uff1a<\/strong>\u786e\u5b9a\u8981\u8c03\u7528\u7684\u65b9\u6cd5\u662f\u5426\u4e3a\u5168\u5c40\u65b9\u6cd5\uff08\u5373\u672a\u5b9a\u4e49\u5728\u4efb\u4f55\u5bf9\u8c61\u4e2d\uff09\u6216\u7279\u5b9a\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/li>\n<li> <strong>\u786e\u5b9a\u8c03\u7528\u65b9\u5f0f\uff1a<\/strong>\u6839\u636e\u65b9\u6cd5\u7684\u5b9a\u4e49\uff0c\u786e\u5b9a\u662f\u76f4\u63a5\u8c03\u7528\u8fd8\u662f\u4f7f\u7528\u70b9\u53f7\u7b26\u53f7\u8c03\u7528\u3002<\/li>\n<li> <strong>\u4f20\u9012\u53c2\u6570\uff1a<\/strong>\u5982\u679c\u65b9\u6cd5\u63a5\u53d7\u53c2\u6570\uff0c\u5c06\u53c2\u6570\u503c\u4f5c\u4e3a\u9017\u53f7\u5206\u9694\u7684\u5217\u8868\u4f20\u9012\u5230\u62ec\u53f7\u5185\u3002<\/li>\n<li> <strong>\u8c03\u7528\u65b9\u6cd5\uff1a<\/strong>\u4f7f\u7528\u65b9\u6cd5\u540d\u79f0\uff08\u53ef\u80fd\u52a0\u4e0a\u70b9\u53f7\u7b26\u53f7\uff09\u548c\u62ec\u53f7\uff0c\u8c03\u7528\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5728 JavaScript \u4e2d\u8c03\u7528\u5168\u5c40\u51fd\u6570\u548c\u5bf9\u8c61\u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ \u8c03\u7528\u5168\u5c40\u51fd\u6570\nconsole.log(\"Hello, JavaScript!\");\n\n\/\/ \u521b\u5efa\u5bf9\u8c61\nconst myObject = {\n  name: \"John Doe\",\n  sayHello: function() {\n    console.log(`Hello, my name is ${this.name}!`);\n  }\n};\n\n\/\/ \u8c03\u7528\u5bf9\u8c61\u65b9\u6cd5\nmyObject.sayHello();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u8c03\u7528\u7684\u65b9\u6cd5\u5df2\u7ecf\u5b9a\u4e49\u597d\uff0c\u5426\u5219\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/li>\n<li>\u5bf9\u4e8e\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0cthis \u5173\u952e\u5b57\u5f15\u7528\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/li>\n<li>JavaScript \u4e2d\u7684\u65b9\u6cd5\u540d\u79f0\u533a\u5206\u5927\u5c0f\u5199\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u8c03\u7528js\u91cc\u7684\u65b9\u6cd5\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\uff0c\u8c03\u7528\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\uff1a\u76f4\u63a5\u8c03\u7528\u5168\u5c40\u65b9\u6cd5\u6216\u53d8\u91cf\u3002\u4f7f\u7528\u70b9\u53f7\u7b26\u53f7\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u65b9\u6cd5\u3002 \u5982\u4f55\u8c03\u7528 JavaScript \u91cc\u9762\u7684\u65b9\u6cd5 \u8c03\u7528 JavaScript \u91cc\u9762\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\uff1a 1. \u76f4\u63a5\u8c03\u7528 \u5bf9\u4e8e\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\u5168\u5c40\u65b9\u6cd5\u6216\u8005\u53d8\u91cf\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\u7684\u540d\u79f0\u8fdb\u884c\u8c03\u7528\uff0c\u65e0\u9700\u4f7f\u7528\u70b9\u53f7\u7b26\u53f7\u6216\u5176\u4ed6\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff1a console.log(&#8220;Hello, world!&#8221;); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5 \u5f53\u65b9\u6cd5\u5c5e\u4e8e\u67d0\u4e2a\u5bf9\u8c61\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u70b9\u53f7\u7b26\u53f7\u5c06\u65b9\u6cd5\u540d\u79f0\u9644\u52a0\u5230\u5bf9\u8c61\u540d\u79f0\u540e\u9762\u8fdb\u884c\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a myObject \u7684\u5bf9\u8c61\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a sayHello \u7684\u65b9\u6cd5\uff1a myObject.sayHello(); \u767b\u5f55\u540e\u590d\u5236 \u4e0b\u9762\u662f JavaScript \u4e2d\u8c03\u7528\u65b9\u6cd5\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a \u627e\u5230\u65b9\u6cd5\u7684\u5b9a\u4e49\uff1a\u786e\u5b9a\u8981\u8c03\u7528\u7684\u65b9\u6cd5\u662f\u5426\u4e3a\u5168\u5c40\u65b9\u6cd5\uff08\u5373\u672a\u5b9a\u4e49\u5728\u4efb\u4f55\u5bf9\u8c61\u4e2d\uff09\u6216\u7279\u5b9a\u5bf9\u8c61\u7684\u5c5e\u6027\u3002 \u786e\u5b9a\u8c03\u7528\u65b9\u5f0f\uff1a\u6839\u636e\u65b9\u6cd5\u7684\u5b9a\u4e49\uff0c\u786e\u5b9a\u662f\u76f4\u63a5\u8c03\u7528\u8fd8\u662f\u4f7f\u7528\u70b9\u53f7\u7b26\u53f7\u8c03\u7528\u3002 \u4f20\u9012\u53c2\u6570\uff1a\u5982\u679c\u65b9\u6cd5\u63a5\u53d7\u53c2\u6570\uff0c\u5c06\u53c2\u6570\u503c\u4f5c\u4e3a\u9017\u53f7\u5206\u9694\u7684\u5217\u8868\u4f20\u9012\u5230\u62ec\u53f7\u5185\u3002 \u8c03\u7528\u65b9\u6cd5\uff1a\u4f7f\u7528\u65b9\u6cd5\u540d\u79f0\uff08\u53ef\u80fd\u52a0\u4e0a\u70b9\u53f7\u7b26\u53f7\uff09\u548c\u62ec\u53f7\uff0c\u8c03\u7528\u65b9\u6cd5\u3002 \u793a\u4f8b\uff1a \u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5728 JavaScript \u4e2d\u8c03\u7528\u5168\u5c40\u51fd\u6570\u548c\u5bf9\u8c61\u65b9\u6cd5\uff1a \/\/ \u8c03\u7528\u5168\u5c40\u51fd\u6570 console.log(&#8220;Hello, JavaScript!&#8221;); \/\/ \u521b\u5efa\u5bf9\u8c61 const myObject = { name: &#8220;John Doe&#8221;, sayHello: function() { console.log(`Hello, my name is [&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-3219","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3219","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=3219"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3219\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}