{"id":3217,"date":"2024-11-10T17:47:52","date_gmt":"2024-11-10T09:47:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3217\/"},"modified":"2024-11-10T17:47:52","modified_gmt":"2024-11-10T09:47:52","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8js%e4%b8%ad%e8%b0%83%e7%94%a8%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3217\/","title":{"rendered":"\u5982\u4f55\u5728js\u4e2d\u8c03\u7528\u51fd\u6570"},"content":{"rendered":"<blockquote><p>\n  javascript \u4e2d\u8c03\u7528\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u4e94\u79cd\uff1a1. \u76f4\u63a5\u8c03\u7528\u51fd\u6570\u540d\uff1b2. \u901a\u8fc7\u53d8\u91cf\u8c03\u7528\u51fd\u6570\uff1b3. \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u51fd\u6570\uff1b4. \u4f5c\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\uff1b5. \u4f7f\u7528 apply() \u6216 call() \u65b9\u6cd5\u6307\u5b9a\u51fd\u6570\u7684 this \u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110807214499496.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728js\u4e2d\u8c03\u7528\u51fd\u6570\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728js\u4e2d\u8c03\u7528\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u8c03\u7528\u51fd\u6570<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u6709\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\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u901a\u8fc7\u51fd\u6570\u540d\u8c03\u7528\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>function myFunction() {\n  console.log(\"Hello, world!\");\n}\n\nmyFunction();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u901a\u8fc7\u53d8\u91cf\u8c03\u7528<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u901a\u8fc7\u53d8\u91cf\u8c03\u7528\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>const myFunction = function() {\n  console.log(\"Hello, world!\");\n};\n\nmyFunction();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<\/strong><\/p>\n<p>\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>function callMyFunction(func) {\n  func();\n}\n\ncallMyFunction(myFunction);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u4f5c\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528<\/strong><\/p>\n<p>\u5982\u679c\u51fd\u6570\u662f\u67d0\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u70b9\u53f7\u6216\u65b9\u62ec\u53f7\u8bed\u6cd5\u8c03\u7528\u5b83\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>const obj = {\n  myFunction: function() {\n    console.log(\"Hello, world!\");\n  },\n};\n\nobj.myFunction();\nobj[\"myFunction\"]();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u4f7f\u7528 apply() \u6216 call() \u65b9\u6cd5<\/strong><\/p>\n<p>apply() \u548c call() \u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u51fd\u6570\u7684 this \u503c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>const myObject = {\n  name: \"John\",\n  sayHello: function() {\n    console.log(`Hello, my name is ${this.name}`);\n  },\n};\n\nmyObject.sayHello(); \/\/ Hello, my name is John\n\nconst anotherObject = {\n  name: \"Jane\",\n};\n\nmyObject.sayHello.apply(anotherObject); \/\/ Hello, my name is Jane<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u9002\u5f53\u7684\u65b9\u6cd5\u8c03\u7528\u51fd\u6570\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u6267\u884c\u548c\u4f20\u9012\u51fd\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728js\u4e2d\u8c03\u7528\u51fd\u6570\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 \u4e2d\u8c03\u7528\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u4e94\u79cd\uff1a1. \u76f4\u63a5\u8c03\u7528\u51fd\u6570\u540d\uff1b2. \u901a\u8fc7\u53d8\u91cf\u8c03\u7528\u51fd\u6570\uff1b3. \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u51fd\u6570\uff1b4. \u4f5c\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\uff1b5. \u4f7f\u7528 apply() \u6216 call() \u65b9\u6cd5\u6307\u5b9a\u51fd\u6570\u7684 this \u503c\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u8c03\u7528\u51fd\u6570 \u5728 JavaScript \u4e2d\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u8c03\u7528\u51fd\u6570\uff1a 1. \u76f4\u63a5\u8c03\u7528 \u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u901a\u8fc7\u51fd\u6570\u540d\u8c03\u7528\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a function myFunction() { console.log(&#8220;Hello, world!&#8221;); } myFunction(); \u767b\u5f55\u540e\u590d\u5236 2. \u901a\u8fc7\u53d8\u91cf\u8c03\u7528 \u4e5f\u53ef\u4ee5\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u901a\u8fc7\u53d8\u91cf\u8c03\u7528\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a const myFunction = function() { console.log(&#8220;Hello, world!&#8221;); }; myFunction(); \u767b\u5f55\u540e\u590d\u5236 3. \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012 \u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a function callMyFunction(func) { func(); } callMyFunction(myFunction); \u767b\u5f55\u540e\u590d\u5236 4. \u4f5c\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528 [&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-3217","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3217","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=3217"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3217\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}