{"id":171021,"date":"2024-05-08T17:20:20","date_gmt":"2024-05-08T09:20:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/171021.html"},"modified":"2024-05-08T17:20:29","modified_gmt":"2024-05-08T09:20:29","slug":"javascript-call%e3%80%81apply%e3%80%81bind-%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/171021.html","title":{"rendered":"JavaScript call\u3001apply\u3001bind \u7684\u533a\u522b\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27041956\/37a7ec21-a439-403e-8262-a730509a868e.webp\" alt=\"JavaScript call\u3001apply\u3001bind \u7684\u533a\u522b\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p>JavaScript\u4e2d\u7684call\u3001apply\u3001\u548cbind\u90fd\u662f\u7528\u4e8e\u6539\u53d8\u51fd\u6570\u8fd0\u884c\u65f6\u7684this\u6307\u5411\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u5728\u51fd\u6570\u8c03\u7528\u6a21\u5f0f\u4e0b\u8d77\u5230\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002<strong>\u5173\u952e\u7684\u533a\u522b\u5728\u4e8e\uff1aCALL\u548cAPPLY\u90fd\u662f\u7acb\u5373\u6267\u884c\u51fd\u6570\uff0c\u4f46\u63a5\u6536\u53c2\u6570\u7684\u65b9\u5f0f\u4e0d\u540c\uff0cBIND\u5219\u8fd4\u56de\u4e00\u4e2a\u6539\u53d8\u4e86\u4e0a\u4e0b\u6587this\u7684\u65b0\u51fd\u6570\uff0c\u5e76\u4e0d\u7acb\u5373\u6267\u884c\u3002<\/strong> \u5176\u4e2d\uff0cCALL\u7684\u529f\u80fd\u662f\u5141\u8bb8\u4e3a\u4e0d\u540c\u7684\u5bf9\u8c61\u5206\u914d\u548c\u8c03\u7528\u5c5e\u4e8e\u4e00\u4e2a\u5bf9\u8c61\u7684\u51fd\u6570\/\u65b9\u6cd5\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u501f\u52a9CALL\u6765\u8c03\u7528\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u4f46\u6267\u884c\u65f6\u5c06\u5176\u4e0a\u4e0b\u6587\u5207\u6362\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001CALL\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>CALL\u65b9\u6cd5\u7684\u9996\u4e2a\u53c2\u6570\u662f\u8981\u7ed1\u5b9a\u7ed9\u51fd\u6570\u7684this\u503c\uff0c\u540e\u7eed\u53c2\u6570\u5219\u76f4\u63a5\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u901a\u8fc7CALL\uff0c\u51fd\u6570\u53ef\u4ee5\u88ab\u8c03\u7528\uff0c\u5e76\u4e14\u5177\u6709\u6307\u5b9a\u7684this\u503c\u548c\u660e\u786e\u7684\u53c2\u6570\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u4e8e\u4f20\u9012\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5bf9\u8c61person\u548c\u4e00\u4e2a\u51fd\u6570displayInfo\uff0c\u53ef\u4ee5\u901a\u8fc7CALL\u65b9\u6cd5\u5c06person\u5bf9\u8c61\u4f5c\u4e3adisplayInfo\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002\u8fd9\u6837\uff0c\u51fd\u6570\u5185\u90e8\u901a\u8fc7this\u5f15\u7528\u7684\u5c31\u662fperson\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function displayInfo(arg1, arg2) {<\/p>\n<p>    console.log(this.name + &quot; &quot; + arg1 + &quot; &quot; + arg2);<\/p>\n<p>}<\/p>\n<p>var person = {<\/p>\n<p>    name: &quot;John&quot;<\/p>\n<p>};<\/p>\n<p>displayInfo.call(person, &quot;Prefers JavaScript&quot;, &quot;Lives in USA&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86CALL\u65b9\u6cd5\u5982\u4f55\u5c06\u4e00\u4e2a\u51fd\u6570\u7684this\u4e0a\u4e0b\u6587\u6539\u4e3a\u7279\u5b9a\u7684\u5bf9\u8c61\uff0c\u5e76\u4f20\u9012\u4e2a\u522b\u7684\u53c2\u6570\u7ed9\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001APPLY\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e0eCALL\u5341\u5206\u76f8\u4f3c\uff0cAPPLY\u7684\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u7b2c\u4e00\u4e2a\u662f\u8981\u7ed1\u5b9a\u7ed9\u51fd\u6570\u7684this\u503c\uff0c\u7b2c\u4e8c\u4e2a\u5219\u662f\u4e00\u4e2a\u53c2\u6570\u6570\u7ec4\u3002\u8fd9\u5bf9\u4e8e\u51fd\u6570\u53c2\u6570\u7684\u6570\u91cf\u4e0d\u786e\u5b9a\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5f53\u4f60\u6709\u4e00\u4e2a\u6570\u7ec4\u5e76\u5e0c\u671b\u7528\u5b83\u7684\u9879\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u65f6\uff0cAPPLY\u5c31\u975e\u5e38\u65b9\u4fbf\u4e86\u3002\u6bd4\u65b9\u8bf4\uff0cMath.max\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u4e00\u7cfb\u5217\u6570\u4e2d\u7684\u6700\u5927\u503c\uff0c\u4f46\u5b83\u4e0d\u652f\u6301\u76f4\u63a5\u4f20\u5165\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cAPPLY\u5c31\u80fd\u6d3e\u4e0a\u7528\u573a\u4e86\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var numbers = [5, 6, 2, 3, 7];<\/p>\n<p>var max = Math.max.apply(null, numbers);<\/p>\n<p>console.log(max);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u901a\u8fc7APPLY\u5c06\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9Math.max\u51fd\u6570\uff0c\u800c\u4e0d\u7528\u62c5\u5fc3\u53c2\u6570\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001BIND\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>BIND\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u7684this\u503c\u4f1a\u88ab\u7ed1\u5b9a\u5230BIND\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u5176\u4f59\u53c2\u6570\u5c06\u4f5c\u4e3a\u65b0\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4f9b\u8c03\u7528\u65f6\u4f7f\u7528\u3002\u4e0eCALL\u548cAPPLY\u4e0d\u540c\uff0cBIND\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\u51fd\u6570\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u6539\u53d8\u4e86\u4e0a\u4e0b\u6587\u7684\u7248\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>BIND\u7279\u522b\u6709\u7528\uff0c\u5f53\u4f60\u9700\u8981\u5ef6\u8fdf\u51fd\u6570\u6267\u884c\u6216\u8005\u5c06\u51fd\u6570\u653e\u5165\u4e8b\u4ef6\u76d1\u542c\u5668\u4e2d\u65f6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u786e\u4fddsetTimeout\u5185\u7684\u51fd\u6570\u80fd\u8bbf\u95ee\u5230\u7279\u5b9a\u4e0a\u4e0b\u6587\u7684this\uff0cBIND\u4fbf\u80fd\u53d1\u6325\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var person = {<\/p>\n<p>    name: &quot;John&quot;,<\/p>\n<p>    delayedGreeting: function() {<\/p>\n<p>        setTimeout(function() {<\/p>\n<p>            console.log(&quot;Hello, &quot; + this.name);<\/p>\n<p>        }.bind(this), 1000);<\/p>\n<p>    }<\/p>\n<p>};<\/p>\n<p>person.delayedGreeting();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7BIND\uff0c\u786e\u4fdd\u4e86setTimeout\u4e2d\u7684\u533f\u540d\u51fd\u6570\u80fd\u591f\u8bbf\u95eeperson\u5bf9\u8c61\u4f5c\u4e3a\u5176\u4e0a\u4e0b\u6587\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u7406\u89e3CALL\u3001APPLY\u548cBIND\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u533a\u522b\uff0c\u5bf9\u4e8e\u7cbe\u786e\u5730\u63a7\u5236\u51fd\u6570\u6267\u884c\u4e2d\u7684this\u7ed1\u5b9a\u6781\u4e3a\u91cd\u8981\u3002\u5b83\u4eec\u5404\u81ea\u6709\u7740\u9002\u5408\u7684\u4f7f\u7528\u573a\u666f\uff0c\u80fd\u591f\u6709\u6548\u5730\u63d0\u5347JavaScript\u7f16\u7a0b\u7684\u7075\u6d3b\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>JavaScript\u7684call\u3001apply\u548cbind\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/p>\n<ul>\n<li><strong>Call\u548cApply\u7528\u4e8e\u8c03\u7528\u51fd\u6570\uff0c\u800cBind\u7528\u4e8e\u521b\u5efa\u65b0\u51fd\u6570\u3002<\/strong> Call\u548cApply\u53ef\u4ee5\u7acb\u5373\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u800cBind\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u7684\u65f6\u5019\u8c03\u7528\u3002<\/li>\n<li><strong>Call\u548cApply\u7684\u4f20\u53c2\u65b9\u5f0f\u4e0d\u540c\u3002<\/strong> Call\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u4e2a\u4f20\u9012\u8fdb\u53bb\u7684\uff0c\u800cApply\u7684\u53c2\u6570\u662f\u4f5c\u4e3a\u6570\u7ec4\u4f20\u9012\u8fdb\u53bb\u7684\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5177\u4f53\u8981\u4f20\u9012\u591a\u5c11\u4e2a\u53c2\u6570\uff0c\u4f7f\u7528Apply\u4f1a\u66f4\u65b9\u4fbf\u3002<\/li>\n<li><strong>Bind\u53ef\u4ee5\u7528\u4e8e\u51fd\u6570\u7ed1\u5b9a\u3002<\/strong> \u4f7f\u7528Bind\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u5bf9\u8c61\u4e0a\uff0c\u8fd9\u610f\u5473\u7740\u7ed1\u5b9a\u540e\u7684\u51fd\u6570\u5c06\u59cb\u7ec8\u4ee5\u7ed1\u5b9a\u5bf9\u8c61\u4f5c\u4e3a\u4e0a\u4e0b\u6587\u6765\u6267\u884c\u3002<\/li>\n<li><strong>Call\u548cApply\u53ef\u4ee5\u6539\u53d8\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\uff0c\u800cBind\u53ea\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002<\/strong> \u5f53\u4f7f\u7528Call\u6216Apply\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u5c06\u66f4\u6539\u4e3a\u6240\u63d0\u4f9b\u7684\u5bf9\u8c61\u3002\u800cBind\u53ea\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5c06\u5176\u4e0a\u4e0b\u6587\u7ed1\u5b9a\u5230\u6307\u5b9a\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8eCall\u548cApply\u53ef\u4ee5\u7acb\u5373\u8c03\u7528\u51fd\u6570\u5e76\u4f20\u9012\u53c2\u6570\uff0c\u800cBind\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5e76\u53ef\u4ee5\u7ed1\u5b9a\u5bf9\u8c61\uff0c\u4f46\u5e76\u4e0d\u7acb\u5373\u6267\u884c\u3002\u5728\u5177\u4f53\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u6539\u53d8\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6216\u4f20\u9012\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u4e2d\u7684call\u3001apply\u3001\u548cbind\u90fd\u662f\u7528\u4e8e\u6539\u53d8\u51fd\u6570\u8fd0\u884c\u65f6\u7684this\u6307\u5411\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u5728\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":171040,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/171021"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=171021"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/171021\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/171040"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=171021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=171021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=171021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}