{"id":2400,"date":"2024-11-10T14:59:44","date_gmt":"2024-11-10T06:59:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2400\/"},"modified":"2024-11-10T14:59:44","modified_gmt":"2024-11-10T06:59:44","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","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2400\/","title":{"rendered":"JavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173072665235230.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\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<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/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<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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\u662fJavaScript \u4e2d\u7684 `this` \u6307\u5411\u95ee\u9898\uff1a\u5982\u4f55\u63a7\u5236\u51fd\u6570\u4e2d\u7684 `this`\uff1f\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>\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\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 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \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 \u5c5e\u6027\u4e0a const boundfunction = f.bind(event); boundfunction(); \/\/ \u8f93\u51fa\u4e8b\u4ef6\u76ee\u6807\u7684 id [&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-2400","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2400","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=2400"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2400\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}