{"id":168137,"date":"2024-05-08T16:17:36","date_gmt":"2024-05-08T08:17:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/168137.html"},"modified":"2024-05-08T16:17:44","modified_gmt":"2024-05-08T08:17:44","slug":"javascript-%e4%b8%ad%e7%9a%84-this-%e6%8c%87%e5%90%91%e9%97%ae%e9%a2%98%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/168137.html","title":{"rendered":"JavaScript \u4e2d\u7684 this \u6307\u5411\u95ee\u9898\u600e\u4e48\u89e3\u51b3"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034252\/00074b84-ffda-4dd4-a7aa-127c3e580440.webp\" alt=\"JavaScript \u4e2d\u7684 this \u6307\u5411\u95ee\u9898\u600e\u4e48\u89e3\u51b3\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u7406\u89e3\u548c\u5904\u7406<code>this<\/code>\u6307\u5411\u95ee\u9898\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u8be5\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u3001\u663e\u5f0f\u7ed1\u5b9a\u3001\u4f7f\u7528apply\u3001call\u548cbind\u65b9\u6cd5<\/strong>\uff0c\u4ee5\u53ca<strong>\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528new\u5173\u952e\u5b57<\/strong>\u3002<strong>\u7bad\u5934\u51fd\u6570<\/strong>\u662f\u5904\u7406<code>this<\/code>\u6307\u5411\u95ee\u9898\u7684\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u4e14\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u7bad\u5934\u51fd\u6570\u4e0d\u7ed1\u5b9a\u81ea\u5df1\u7684<code>this<\/code>\uff0c\u5b83\u4eec\u4f1a\u6355\u83b7\u5176\u6240\u5728\u4e0a\u4e0b\u6587\u7684<code>this<\/code>\u503c\uff0c\u4f5c\u4e3a\u81ea\u5df1\u7684<code>this<\/code>\u503c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u4f20\u7edf\u51fd\u6570\u4e2d<code>this<\/code>\u7684\u52a8\u6001\u53d8\u5316\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u89e3\u51b3this\u6307\u5411\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u7684\u5f15\u5165\uff0c\u662fES6\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u51fd\u6570\u5199\u6cd5\uff0c\u5e76\u4e14\u6539\u53d8\u4e86<code>this<\/code>\u7684\u7ed1\u5b9a\u884c\u4e3a\u3002\u5728\u7bad\u5934\u51fd\u6570\u4e2d\uff0c<code>this<\/code>\u88ab\u81ea\u52a8\u7ed1\u5b9a\u5230\u5b83\u6240\u5728\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u5728\u7bad\u5934\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>this<\/code>\u65f6\uff0c\u5b83\u6307\u5411\u7684\u662f\u5b9a\u4e49\u65f6\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61\uff0c\u800c\u975e\u8fd0\u884c\u65f6\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u56de\u8c03\u51fd\u6570\u548c\u4e8b\u4ef6\u5904\u7406\u5668\u4e2d\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u4f20\u7edf\u51fd\u6570\u4e2d\u56e0\u4e3a\u51fd\u6570\u88ab\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u6216\u52a8\u6001\u5206\u914d\u800c\u5bfc\u81f4<code>this<\/code>\u4e22\u5931\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u76d1\u542c\u4e8b\u4ef6\u6216\u8bbe\u7f6e\u5b9a\u65f6\u5668\u65f6\uff0c\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u786e\u4fdd<code>this<\/code>\u7684\u6b63\u786e\u6307\u5411\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u4f20\u7edf\u533f\u540d\u51fd\u6570\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u663e\u5f0f\u7ed1\u5b9athis<\/h3>\n<\/p>\n<p><p>\u663e\u5f0f\u7ed1\u5b9a\u662f\u901a\u8fc7\u4f7f\u7528<code>Function.prototype<\/code>\u4e0a\u7684<code>call<\/code>\u3001<code>apply<\/code>\u6216<code>bind<\/code>\u65b9\u6cd5\u6765\u660e\u786e\u6307\u5b9a\u51fd\u6570\u8c03\u7528\u65f6<code>this<\/code>\u7684\u503c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u662f<code>this<\/code>\u5e94\u5f53\u7ed1\u5b9a\u7684\u5bf9\u8c61\uff0c<code>call<\/code>\u548c<code>apply<\/code>\u7684\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u4f20\u9012\u53c2\u6570\u7684\u65b9\u5f0f\u4e0d\u540c\uff0c\u800c<code>bind<\/code>\u65b9\u6cd5\u5219\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u7684<code>this<\/code>\u88ab\u6c38\u4e45\u7ed1\u5b9a\u4e3a<code>bind<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528call\u548capply<\/strong>\u65f6\uff0c\u53ef\u4ee5\u89e3\u51b3\u56de\u8c03\u51fd\u6570\u4e2d<code>this<\/code>\u4e22\u5931\u7684\u95ee\u9898\uff0c\u901a\u8fc7\u5728\u51fd\u6570\u8c03\u7528\u65f6\u663e\u5f0f\u5730\u5c06<code>this<\/code>\u7ed1\u5b9a\u81f3\u6b63\u786e\u7684\u5bf9\u8c61\uff0c\u786e\u4fdd\u51fd\u6570\u8fd0\u884c\u65f6<code>this<\/code>\u7684\u51c6\u786e\u6027\u3002<\/li>\n<li><strong>\u4f7f\u7528bind<\/strong>\u5219\u66f4\u591a\u7528\u4e8e\u9700\u8981\u9884\u5148\u8bbe\u7f6e<code>this<\/code>\u503c\u7684\u60c5\u51b5\uff0c\u751f\u6210\u7684\u662f\u4e00\u4e2a\u5df2\u7ecf\u7ed1\u5b9a\u4e86\u7279\u5b9a<code>this<\/code>\u503c\u7684\u65b0\u51fd\u6570\uff0c\u975e\u5e38\u9002\u7528\u4e8e\u4e8b\u4ef6\u5904\u7406\u548c\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u6784\u9020\u51fd\u6570\u548cnew\u5173\u952e\u5b57\u7ed1\u5b9a<\/h3>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u8c03\u7528\u6784\u9020\u51fd\u6570\u65f6\uff0cJavaScript\u4f1a\u81ea\u52a8\u4e3a\u8fd9\u4e2a\u65b0\u5bf9\u8c61\u7ed1\u5b9a<code>this<\/code>\u3002\u6784\u9020\u51fd\u6570\u4e2d\u7684<code>this<\/code>\u6307\u5411\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u8fd9\u5bf9\u4e8e\u521b\u5efa\u5e76\u521d\u59cb\u5316\u5bf9\u8c61\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528<code>this<\/code>\uff0c\u53ef\u4ee5\u5c06\u5c5e\u6027\u548c\u65b9\u6cd5\u7ed1\u5b9a\u5230\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u4e0a\u3002\u6b64\u65f6\uff0c<code>this<\/code>\u7684\u6307\u5411\u975e\u5e38\u660e\u786e\uff0c\u5373\u6307\u5411\u901a\u8fc7<code>new<\/code>\u5173\u952e\u5b57\u521b\u5efa\u7684\u65b0\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6784\u9020\u51fd\u6570\u65f6\u9700\u8981\u6ce8\u610f\uff0c\u5728\u51fd\u6570\u5185\u90e8\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u6539\u53d8<code>this<\/code>\u7684\u7ed1\u5b9a\u5bf9\u8c61\uff0c\u5982\u679c\u6784\u9020\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c<code>new<\/code>\u64cd\u4f5c\u5c06\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4e4b\u524d\u901a\u8fc7<code>new<\/code>\u521b\u5efa\u7684\u90a3\u4e2a\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u89e3\u51b3JavaScript\u4e2d\u7684<code>this<\/code>\u6307\u5411\u95ee\u9898\uff0c\u9700\u8981\u7075\u6d3b\u8fd0\u7528\u7bad\u5934\u51fd\u6570\u3001\u663e\u5f0f\u7ed1\u5b9a\uff08<code>call<\/code>\u3001<code>apply<\/code>\u548c<code>bind<\/code>\uff09\u4ee5\u53ca\u6784\u9020\u51fd\u6570\u4e0e<code>new<\/code>\u5173\u952e\u5b57\u3002\u7406\u89e3\u5404\u79cd\u7ed1\u5b9a\u673a\u5236\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5bf9\u4e8e\u7f16\u5199\u9ad8\u8d28\u91cf\u7684JavaScript\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u6709\u52a9\u4e8e\u5f00\u53d1\u8005\u6709\u6548\u5730\u89e3\u51b3<code>this<\/code>\u76f8\u5173\u7684\u95ee\u9898\uff0c\u7f16\u5199\u51fa\u66f4\u52a0\u53ef\u9760\u548c\u7ef4\u62a4\u6027\u9ad8\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: \u5982\u4f55\u89e3\u51b3JavaScript\u4e2d\u7684this\u6307\u5411\u95ee\u9898\uff1f<\/strong><br \/>\nA1: JavaScript\u4e2d\u7684this\u6307\u5411\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u6216\u7ed1\u5b9athis\u6765\u89e3\u51b3\u3002\u7bad\u5934\u51fd\u6570\u4e0d\u7ed1\u5b9a\u81ea\u5df1\u7684this\uff0c\u800c\u662f\u7ee7\u627f\u5916\u90e8\u51fd\u6570\u7684this\u3002\u53e6\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528bind\u3001call\u6216apply\u624b\u52a8\u7ed1\u5b9athis\uff0c\u5c06\u51fd\u6570\u4e0e\u6307\u5b9a\u7684\u5bf9\u8c61\u8fdb\u884c\u7ed1\u5b9a\u3002<\/p>\n<p><strong>Q2: \u5728JavaScript\u4e2d\uff0c\u5982\u4f55\u5904\u7406this\u6307\u5411\u9519\u8bef\u7684\u95ee\u9898\uff1f<\/strong><br \/>\nA2: \u5f53this\u6307\u5411\u9519\u8bef\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u4f5c\u7528\u57df\u4e2d\u7f13\u5b58\u6b63\u786e\u7684this\u5e76\u5728\u9700\u8981\u65f6\u4f7f\u7528\u8be5\u53d8\u91cf\u6765\u89e3\u51b3\u95ee\u9898\u3002\u53e6\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4eec\u7ee7\u627f\u5916\u90e8\u51fd\u6570\u7684this\uff0c\u53ef\u4ee5\u907f\u514dthis\u6307\u5411\u9519\u8bef\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>Q3: \u5982\u4f55\u907f\u514dJavaScript\u4e2d\u7684this\u6307\u5411\u6df7\u4e71\u95ee\u9898\uff1f<\/strong><br \/>\nA3: \u8981\u907f\u514dJavaScript\u4e2dthis\u6307\u5411\u6df7\u4e71\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u800c\u4e0d\u662f\u666e\u901a\u51fd\u6570\u3002\u53e6\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528bind\u3001call\u6216apply\u6765\u663e\u5f0f\u5730\u7ed1\u5b9athis\u3002\u6b64\u5916\uff0c\u5728\u53c2\u4e0e\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528event.currentTarget\u6765\u5f15\u7528\u5f53\u524d\u64cd\u4f5c\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56this\u6307\u5411\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u8981\u5c0f\u5fc3\u4f7f\u7528this\uff0c\u5e76\u786e\u4fdd\u4e86\u89e3\u5176\u6307\u5411\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\uff0c\u7406\u89e3\u548c\u5904\u7406this\u6307\u5411\u95ee\u9898\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u8be5\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u3001\u663e\u5f0f\u7ed1 [&hellip;]","protected":false},"author":3,"featured_media":168151,"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\/168137"}],"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=168137"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168137\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/168151"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=168137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=168137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=168137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}