{"id":53458,"date":"2024-12-03T12:47:26","date_gmt":"2024-12-03T04:47:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53458\/"},"modified":"2024-12-03T12:47:26","modified_gmt":"2024-12-03T04:47:26","slug":"javascript-%e4%b8%ad%e7%9a%84-this-%e6%8c%87%e5%90%91%e9%97%ae%e9%a2%98%ef%bc%9a%e5%a6%82%e4%bd%95%e6%8e%a7%e5%88%b6%e5%87%bd%e6%95%b0%e4%b8%ad%e7%9a%84-this%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53458\/","title":{"rendered":"JavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f<\/h1>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241130\/1732962472674ae8a812bc4.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u63a7\u5236 javascript \u51fd\u6570\u4e2d this \u7684\u6307\u5411<\/strong><\/p>\n<p>\u5728 javascript \u51fd\u6570\u4e2d\uff0c<strong>this<\/strong> \u5173\u952e\u5b57\u6307\u5411\u4e0d\u540c\u7684\u4e8b\u7269\u4f1a\u5f15\u8d77\u56f0\u6270\u3002\u5f53\u7ed1\u5b9a\u5230\u5143\u7d20\u65f6\uff0c<strong>this<\/strong> \u6307\u5411\u8be5\u5143\u7d20\u3002\u5f53\u7ed1\u5b9a\u5168\u5c40\u4f5c\u7528\u57df\u65f6\uff0c<strong>this<\/strong> \u6307\u5411 window \u5bf9\u8c61\u3002<\/p>\n<p>\u5bf9\u4e8e\u4f60\u7684\u95ee\u9898\uff1a&#8221;function f() { alert($(this).attr(&#8216;id&#8217;)); }&#8221; \u4e2d\uff0c<strong>this<\/strong> \u6709\u65f6\u6307\u5411\u70b9\u51fb\u7684\u5143\u7d20\uff0c\u6709\u65f6\u6307\u5411\u51fd\u6570\u672c\u8eab\u3002\u8fd9\u662f\u56e0\u4e3a javascript \u7684\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u3002\u5f53\u5143\u7d20\u89e6\u53d1\u4e8b\u4ef6\u65f6\uff0c<strong>this<\/strong> \u4f1a\u88ab\u9690\u5f0f\u7ed1\u5b9a\u5230\u4e8b\u4ef6\u6e90\u5143\u7d20\u3002\u7136\u800c\uff0c\u5f53\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u65f6\uff0c<strong>this<\/strong> \u4f1a\u6307\u5411\u5168\u5c40\u4f5c\u7528\u57df\uff08window \u5bf9\u8c61\uff09\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528 bind \u65b9\u6cd5\uff1a<\/strong> bind \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u5e76\u5c06\u5176 <strong>this<\/strong> \u503c\u56fa\u5b9a\u4e3a\u7ed9\u5b9a\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>function f(event) {\n  console.log(event.target.id);\n}\n\n\/\/ \u5c06 f \u51fd\u6570\u7ed1\u5b9a\u5230 event \u5bf9\u8c61\u7684 target \u5c5e\u6027\u4e0a\nconst boundfunction = f.bind(event);\nboundfunction(); \/\/ \u8f93\u51fa\u4e8b\u4ef6\u76ee\u6807\u7684 id<\/pre>\n<ul>\n<li><strong>\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff1a<\/strong> \u7bad\u5934\u51fd\u6570\u9690\u5f0f\u7ed1\u5b9a <strong>this<\/strong> \u5230\u5176\u7236\u4f5c\u7528\u57df\u7684 <strong>this<\/strong> \u503c\u3002\u56e0\u6b64\uff0c\u65e0\u8bba\u5982\u4f55\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff0c<strong>this<\/strong> \u59cb\u7ec8\u6307\u5411\u7236\u4f5c\u7528\u57df\u7684 <strong>this<\/strong> \u503c\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>function f() {\n  console.log(this.id);\n}\n\n$(\"button\").click(function() {\n  f.call(this); \/\/ \u8f93\u51fa\u6309\u94ae\u7684 id\n});<\/pre>\n<ul>\n<li><strong>\u5c06\u4e8b\u4ef6\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff1a<\/strong> \u5982\u679c\u51fd\u6570\u9700\u8981\u5728\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u4e2d\u8fd0\u884c\uff0c\u53ef\u4ee5\u5c06\u4e8b\u4ef6\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u7136\u540e\u4f7f\u7528 event.target \u83b7\u53d6\u89e6\u53d1\u4e8b\u4ef6\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>function f(event) {\n  console.log(event.target.id);\n}\n\n$(\"button\").click(f);<\/pre>\n<p>\u4e0a\u8ff0\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u6765\u63a7\u5236 javascript \u51fd\u6570\u4e2d <strong>this<\/strong> \u7684\u6307\u5411\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u9700\u6c42\u548c\u8bbe\u8ba1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aJavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u540c\u4e00\u6d4f\u89c8\u5668\u7248\u672c\u4e0b\uff0c\u6eda\u52a8\u6761\u6837\u5f0f\u4e3a\u4f55\u4e0d\u540c\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f \u5b66\u4e60\u6587\u7ae0\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0\u300aJavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f\u300b\u5c06\u4f1a\u4ecb\u7ecd\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60\u6587\u7ae0\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01 \u5982\u4f55\u63a7\u5236 javascript \u51fd\u6570\u4e2d this \u7684\u6307\u5411 \u5728 javascript \u51fd\u6570\u4e2d\uff0cthis \u5173\u952e\u5b57\u6307\u5411\u4e0d\u540c\u7684\u4e8b\u7269\u4f1a\u5f15\u8d77\u56f0\u6270\u3002\u5f53\u7ed1\u5b9a\u5230\u5143\u7d20\u65f6\uff0cthis \u6307\u5411\u8be5\u5143\u7d20\u3002\u5f53\u7ed1\u5b9a\u5168\u5c40\u4f5c\u7528\u57df\u65f6\uff0cthis \u6307\u5411 window \u5bf9\u8c61\u3002 \u5bf9\u4e8e\u4f60\u7684\u95ee\u9898\uff1a&#8221;function f() { alert($(this).attr(&#8216;id&#8217;)); }&#8221; \u4e2d\uff0cthis \u6709\u65f6\u6307\u5411\u70b9\u51fb\u7684\u5143\u7d20\uff0c\u6709\u65f6\u6307\u5411\u51fd\u6570\u672c\u8eab\u3002\u8fd9\u662f\u56e0\u4e3a javascript \u7684\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u3002\u5f53\u5143\u7d20\u89e6\u53d1\u4e8b\u4ef6\u65f6\uff0cthis \u4f1a\u88ab\u9690\u5f0f\u7ed1\u5b9a\u5230\u4e8b\u4ef6\u6e90\u5143\u7d20\u3002\u7136\u800c\uff0c\u5f53\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u65f6\uff0cthis \u4f1a\u6307\u5411\u5168\u5c40\u4f5c\u7528\u57df\uff08window \u5bf9\u8c61\uff09\u3002 \u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u4f7f\u7528 bind \u65b9\u6cd5\uff1a bind \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u5e76\u5c06\u5176 this \u503c\u56fa\u5b9a\u4e3a\u7ed9\u5b9a\u7684\u503c\u3002\u4f8b\u5982\uff1a function f(event) { console.log(event.target.id); } \/\/ \u5c06 f \u51fd\u6570\u7ed1\u5b9a\u5230 event \u5bf9\u8c61\u7684 target [&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-53458","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53458","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=53458"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53458\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}