{"id":3411,"date":"2024-11-10T13:09:18","date_gmt":"2024-11-10T05:09:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3411\/"},"modified":"2024-11-10T13:09:18","modified_gmt":"2024-11-10T05:09:18","slug":"js%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8js%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3411\/","title":{"rendered":"js\u5982\u4f55\u8c03\u7528js\u65b9\u6cd5"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u8c03\u7528 js \u65b9\u6cd5\u7684\u65b9\u6cd5\u6709\u4e09\u6b65\uff1a1. \u5b9a\u4e49\u65b9\u6cd5\uff1b2. \u4f7f\u7528\u70b9\u8bed\u6cd5\uff08\u5168\u5c40\u5bf9\u8c61\uff09\u6216\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff08\u5bf9\u8c61\uff09\u8c03\u7528\u65b9\u6cd5\uff1b3. \u53ef\u9009\u4f20\u9012\u53c2\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110811482592690.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u8c03\u7528js\u65b9\u6cd5\u63d2\u56fe\" alt=\"js\u5982\u4f55\u8c03\u7528js\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<h2>\u5982\u4f55\u5728 JS \u4e2d\u8c03\u7528 JS \u65b9\u6cd5\uff1f<\/h2>\n<p>\u5728 JavaScript \u4e2d\u8c03\u7528 JS \u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<h3>1. \u5b9a\u4e49\u65b9\u6cd5<\/h3>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u8981\u8c03\u7528\u7684 JS \u65b9\u6cd5\u3002\u65b9\u6cd5\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u63a5\u6536\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002<\/p>\n<pre>\/\/ \u5728 global \u8303\u56f4\u5185\u5b9a\u4e49\u65b9\u6cd5\nfunction myMethod() {\n  \/\/ \u65b9\u6cd5\u7684\u903b\u8f91\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>2. \u8c03\u7528\u65b9\u6cd5<\/h3>\n<p>\u8981\u8c03\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 dot \u8bed\u6cd5\u6216\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff1a<\/p>\n<p><strong>\u4f7f\u7528 dot \u8bed\u6cd5\uff08\u5bf9\u4e8e\u5168\u5c40\u5bf9\u8c61\uff09\uff1a<\/strong><\/p>\n<pre>myMethod(); \/\/ \u8c03\u7528\u5168\u5c40\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff08\u5bf9\u4e8e\u5bf9\u8c61\uff09\uff1a<\/strong><\/p>\n<pre>let obj = {\n  myMethod: function() {\n    \/\/ \u65b9\u6cd5\u7684\u903b\u8f91\n  }\n};\n\nobj.myMethod(); \/\/ \u8c03\u7528\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>3. \u4f20\u9012\u53c2\u6570\uff08\u53ef\u9009\uff09<\/h3>\n<p>\u5982\u679c\u65b9\u6cd5\u9700\u8981\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u65b9\u6cd5\u65f6\u4f20\u9012\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<pre>myMethod(arg1, arg2); \/\/ \u4f20\u9012\u53c2\u6570<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5b9a\u4e49\u548c\u8c03\u7528\u4e00\u4e2a JavaScript \u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ \u5b9a\u4e49\u65b9\u6cd5\nfunction sayHello(name) {\n  console.log(`\u4f60\u597d\uff0c${name}\uff01`);\n}\n\n\/\/ \u8c03\u7528\u65b9\u6cd5\nsayHello(\"John\"); \/\/ \u8f93\u51fa\uff1a\u4f60\u597d\uff0cJohn\uff01<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u9644\u52a0\u8bf4\u660e<\/h3>\n<ul>\n<li>\u65b9\u6cd5\u53ef\u4ee5\u662f\u533f\u540d\u51fd\u6570\uff08\u6ca1\u6709\u540d\u79f0\uff09\u3002<\/li>\n<li>\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 return \u5173\u952e\u5b57\u6765\u6307\u5b9a\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 this \u5173\u952e\u5b57\u6765\u8bbf\u95ee\u65b9\u6cd5\u6240\u5728\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u8c03\u7528js\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\u8c03\u7528 js \u65b9\u6cd5\u7684\u65b9\u6cd5\u6709\u4e09\u6b65\uff1a1. \u5b9a\u4e49\u65b9\u6cd5\uff1b2. \u4f7f\u7528\u70b9\u8bed\u6cd5\uff08\u5168\u5c40\u5bf9\u8c61\uff09\u6216\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff08\u5bf9\u8c61\uff09\u8c03\u7528\u65b9\u6cd5\uff1b3. \u53ef\u9009\u4f20\u9012\u53c2\u6570\u3002 \u5982\u4f55\u5728 JS \u4e2d\u8c03\u7528 JS \u65b9\u6cd5\uff1f \u5728 JavaScript \u4e2d\u8c03\u7528 JS \u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u5b9a\u4e49\u65b9\u6cd5 \u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u8981\u8c03\u7528\u7684 JS \u65b9\u6cd5\u3002\u65b9\u6cd5\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u63a5\u6536\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002 \/\/ \u5728 global \u8303\u56f4\u5185\u5b9a\u4e49\u65b9\u6cd5 function myMethod() { \/\/ \u65b9\u6cd5\u7684\u903b\u8f91 } \u767b\u5f55\u540e\u590d\u5236 2. \u8c03\u7528\u65b9\u6cd5 \u8981\u8c03\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 dot \u8bed\u6cd5\u6216\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff1a \u4f7f\u7528 dot \u8bed\u6cd5\uff08\u5bf9\u4e8e\u5168\u5c40\u5bf9\u8c61\uff09\uff1a myMethod(); \/\/ \u8c03\u7528\u5168\u5c40\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5 \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff08\u5bf9\u4e8e\u5bf9\u8c61\uff09\uff1a let obj = { myMethod: function() { \/\/ [&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-3411","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3411","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=3411"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3411\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}