{"id":88179,"date":"2024-04-07T17:25:05","date_gmt":"2024-04-07T09:25:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/88179.html"},"modified":"2024-04-07T17:25:06","modified_gmt":"2024-04-07T09:25:06","slug":"javascript%e4%b8%ad%e7%9a%84this%e6%8c%87%e5%90%91%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/88179.html","title":{"rendered":"JavaScript\u4e2d\u7684this\u6307\u5411\u95ee\u9898"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02172633\/e70e9aad-536e-4ea5-a037-684d2cea8eb2.webp\" alt=\"JavaScript\u4e2d\u7684this\u6307\u5411\u95ee\u9898\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c<code>this<\/code>\u5173\u952e\u5b57\u662f\u4e00\u4e2a\u52a8\u6001\u7ed1\u5b9a\u7684\u6982\u5ff5\uff0c\u5b83\u7684\u6307\u5411\u4f9d\u8d56\u4e8e\u51fd\u6570\u7684\u8c03\u7528\u65b9\u5f0f\u3002\u5927\u81f4\u6765\u8bf4\uff0c<strong><code>this<\/code>\u53ef\u4ee5\u6307\u5411\u5168\u5c40\u5bf9\u8c61\u3001\u5f53\u524d\u5b9e\u4f8b\u5bf9\u8c61\u3001\u6307\u5b9a\u7684\u5bf9\u8c61<\/strong>\uff0c\u6216\u5728<strong>\u7bad\u5934\u51fd\u6570<\/strong>\u4e2d\u4fdd\u6301\u8bcd\u6cd5\u4f5c\u7528\u57df\u3002<strong>\u91cd\u70b9\u89e3\u91ca<\/strong>\uff0c\u5728\u666e\u901a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c<code>this<\/code>\u4e00\u822c\u6307\u5411\u5168\u7403\u5bf9\u8c61\uff08\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u4e3a<code>undefined<\/code>\uff09\u3002\u4f46\u5728\u65b9\u6cd5\u8c03\u7528\u3001\u6784\u9020\u51fd\u6570\u53caClass\u4e2d\uff0c\u5219\u8868\u73b0\u51fa\u66f4\u4e3a\u590d\u6742\u7684\u884c\u4e3a\u3002\u7279\u522b\u662f\u5728<strong>\u7bad\u5934\u51fd\u6570<\/strong>\u4e2d\uff0c<code>this<\/code>\u7684\u6307\u5411\u662f\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u7ed1\u5b9a\u7684\uff0c\u800c\u4e0d\u662f\u5728\u51fd\u6570\u8c03\u7528\u65f6\u7ed1\u5b9a\u7684\uff0c\u8fd9\u4e0e\u666e\u901a\u51fd\u6570\u5f62\u6210\u4e86\u9c9c\u660e\u5bf9\u6bd4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u666e\u901a\u51fd\u6570\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u672a\u7ecf\u7ed1\u5b9a\u7684\u666e\u901a\u51fd\u6570\u8c03\u7528\u4e2d\uff0c<code>this<\/code>\u6307\u5411\u7684\u662f\u5168\u5c40\u5bf9\u8c61\uff08\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\u662f<code>window<\/code>\uff0c\u5728Node.js\u4e2d\u662f<code>global<\/code>\uff09\u3002\u800c\u5728\u4e25\u683c\u6a21\u5f0f(<code>use strict<\/code>)\u4e0b\uff0c<code>this<\/code>\u7684\u503c\u5c06\u4f1a\u662f<code>undefined<\/code>\u3002\u8fd9\u79cd\u884c\u4e3a\u7684\u5dee\u5f02\u4f1a\u5f15\u8d77\u51fd\u6570\u5185\u90e8<code>this<\/code>\u4f7f\u7528\u7684\u4e0d\u786e\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function show() {<\/p>\n<p>  console.log(this);<\/p>\n<p>}<\/p>\n<p>show(); \/\/ \u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\u5c06\u663e\u793a\u5168\u5c40\u5bf9\u8c61\uff0c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u663e\u793aundefined<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u65b9\u6cd5\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5f53\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c<code>this<\/code>\u6307\u5411\u8be5\u5bf9\u8c61\u3002\u8fd9\u5bf9\u4e8e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u65b9\u6cd5\u52a8\u6001\u8bbf\u95ee\u5176\u5c5e\u4e8e\u7684\u5bf9\u8c61\u7684\u5c5e\u6027\u6216\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const obj = {<\/p>\n<p>  name: &#039;\u793a\u4f8b\u5bf9\u8c61&#039;,<\/p>\n<p>  showName() {<\/p>\n<p>    console.log(this.name);<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p>obj.showName(); \/\/ \u8f93\u51fa\uff1a\u793a\u4f8b\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6784\u9020\u51fd\u6570\u4e0eClass<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u8c03\u7528\u6784\u9020\u51fd\u6570\u65f6\uff0c<code>this<\/code>\u5c06\u6307\u5411\u4e00\u4e2a\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u8fd9\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u7279\u5f81\uff0c\u4f7f\u5f97\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u521d\u59cb\u5316\u65b0\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Person(name) {<\/p>\n<p>  this.name = name;<\/p>\n<p>}<\/p>\n<p>const person = new Person(&#039;\u5f20\u4e09&#039;);<\/p>\n<p>console.log(person.name); \/\/ \u8f93\u51fa\uff1a\u5f20\u4e09<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728ES6\u7684Class\u4e2d\uff0c<code>this<\/code>\u7684\u884c\u4e3a\u4e0e\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\uff0c\u6307\u5411\u5b9e\u4f8b\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">class Person {<\/p>\n<p>  constructor(name) {<\/p>\n<p>    this.name = name;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>const person = new Person(&#039;\u674e\u56db&#039;);<\/p>\n<p>console.log(person.name); \/\/ \u8f93\u51fa\uff1a\u674e\u56db<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7bad\u5934\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4e0d\u7ed1\u5b9a<code>this<\/code>\uff0c\u5b83\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\u56e0\u6b64\u7bad\u5934\u51fd\u6570\u5185\u7684<code>this<\/code>\u662f\u4e0d\u4f1a\u968f\u7740\u8c03\u7528\u65b9\u5f0f\u7684\u6539\u53d8\u800c\u6539\u53d8\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const obj = {<\/p>\n<p>  name: &#039;\u793a\u4f8b\u5bf9\u8c61&#039;,<\/p>\n<p>  showName: () =&gt; {<\/p>\n<p>    console.log(this.name);<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p>obj.showName(); \/\/ \u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\uff0c\u65e0\u8bba\u5982\u4f55\u8c03\u7528\uff0c\u603b\u662f\u8f93\u51fa\u7a7a\uff0c\u56e0\u4e3a\u7bad\u5934\u51fd\u6570\u5185\u90e8\u7684this\u7ed1\u5b9a\u4e86\u5168\u5c40\u4f5c\u7528\u57df<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7279\u6027\u4f7f\u5f97\u7bad\u5934\u51fd\u6570\u7279\u522b\u9002\u5408\u4e8e\u90a3\u4e9b\u9700\u8981\u4fdd\u8bc1<code>this<\/code>\u4e0d\u53d8\u7684\u573a\u666f\uff0c\u6bd4\u5982\u4e8b\u4ef6\u56de\u8c03\u3001\u5b9a\u65f6\u5668\u4e2d\u7684\u51fd\u6570\uff0c\u6216\u8005\u90a3\u4e9b\u5e76\u4e0d\u671f\u671b<code>this<\/code>\u88ab\u968f\u610f\u6539\u53d8\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u663e\u5f0f\u7ed1\u5b9a<\/h3>\n<\/p>\n<p><p>JavaScript\u63d0\u4f9b\u4e86<code>Function.prototype.bind<\/code>\u3001<code>call<\/code>\u3001<code>apply<\/code>\u4e09\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u5f0f\u5730\u8bbe\u7f6e\u51fd\u6570\u8c03\u7528\u65f6<code>this<\/code>\u7684\u6307\u5411\u3002<\/p>\n<\/p>\n<ul>\n<li><strong><code>bind<\/code>\u65b9\u6cd5<\/strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u5728\u8c03\u7528\u65f6\u8bbe\u7f6e<code>this<\/code>\u5173\u952e\u5b57\u4e3a\u63d0\u4f9b\u7684\u503c\u3002<\/li>\n<li><strong><code>call<\/code>\u548c<code>apply<\/code>\u65b9\u6cd5<\/strong>\u5219\u662f\u7acb\u5373\u6267\u884c\u51fd\u6570\uff0c\u5e76\u5141\u8bb8\u4e3a\u51fd\u6570\u6267\u884c\u8bbe\u7f6e<code>this<\/code>\u3002<\/li>\n<\/ul>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u4e3a\u52a8\u6001\u6539\u53d8\u51fd\u6570\u6267\u884c\u65f6<code>this<\/code>\u7684\u6307\u5411\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u624b\u6bb5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function showName() {<\/p>\n<p>  console.log(this.name);<\/p>\n<p>}<\/p>\n<p>const obj = { name: &#039;\u793a\u4f8b\u5bf9\u8c61&#039; };<\/p>\n<p>const boundShowName = showName.bind(obj);<\/p>\n<p>boundShowName(); \/\/ \u8f93\u51fa\uff1a\u793a\u4f8b\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\u800c\u8a00\uff0c\u7406\u89e3\u548c\u6b63\u786e\u5730\u4f7f\u7528JavaScript\u4e2d\u7684<code>this<\/code>\u5173\u952e\u5b57\uff0c\u5bf9\u4e8e\u5f00\u53d1\u590d\u6742\u7684JavaScript\u5e94\u7528\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u51e0\u79cd\u6700\u5e38\u89c1\u7684\u60c5\u5f62\uff0c\u4f46\u8fd8\u6709\u5f88\u591a\u7279\u6b8a\u60c5\u51b5\u6216\u8fb9\u7f18\u6848\u4f8b\u53ef\u80fd\u9700\u8981\u7279\u522b\u7684\u5904\u7406\u3002\u638c\u63e1\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\uff0c\u5c06\u6709\u52a9\u4e8e\u5f00\u53d1\u8005\u7f16\u5199\u66f4\u52a0\u5065\u58ee\u548c\u7075\u6d3b\u7684JavaScript\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<ol>\n<li>\n<p><strong>\u5728JavaScript\u4e2d\uff0cthis\u6307\u5411\u7684\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\n\u5728JavaScript\u4e2d\uff0cthis\u5173\u952e\u5b57\u901a\u5e38\u6307\u5411\u5f53\u524d\u6267\u884c\u7684\u4e0a\u4e0b\u6587\u3002\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u53ef\u80fd\u662f\u4e00\u4e2a\u5bf9\u8c61\u3001\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u5168\u5c40\u73af\u5883\u3002\u5f53\u51fd\u6570\u4f5c\u4e3a\u65b9\u6cd5\u8c03\u7528\u65f6\uff0cthis\u4f1a\u6307\u5411\u8fd9\u4e2a\u65b9\u6cd5\u6240\u5c5e\u7684\u5bf9\u8c61\u3002\u800c\u5728\u5168\u5c40\u73af\u5883\u4e2d\uff0cthis\u6307\u5411\u7684\u662f\u5168\u5c40\u5bf9\u8c61\uff08\u5728\u6d4f\u89c8\u5668\u4e2d\u4e3awindow\u5bf9\u8c61\uff09\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5728JavaScript\u4e2d\u6b63\u786e\u5730\u4f7f\u7528this\u5173\u952e\u5b57\uff1f<\/strong><br \/>\n\u4f7f\u7528this\u5173\u952e\u5b57\u7684\u6b63\u786e\u65b9\u5f0f\u53d6\u51b3\u4e8e\u4e0a\u4e0b\u6587\u3002\u5f53\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u65f6\uff0cthis\u901a\u5e38\u6307\u5411\u8fd9\u4e2a\u5bf9\u8c61\u3002\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u5f53\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u65f6\uff0cthis\u4f1a\u7ee7\u627f\u81ea\u5916\u90e8\u4f5c\u7528\u57df\uff0c\u800c\u4e0d\u662f\u6307\u5411\u5bf9\u8c61\u672c\u8eab\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528Function.prototype.call()\u3001Function.prototype.apply()\u6216Function.prototype.bind()\u6765\u663e\u5f0f\u5730\u8bbe\u7f6e\u51fd\u6570\u5185\u90e8\u7684this\u503c\u3002<\/p>\n<\/li>\n<li>\n<p><strong>JavaScript\u4e2d\u7684this\u6307\u5411\u95ee\u9898\u5728\u95ed\u5305\u4e2d\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\n\u5728\u95ed\u5305\u4e2d\uff0cthis\u7684\u6307\u5411\u53ef\u80fd\u4f1a\u51fa\u73b0\u95ee\u9898\u3002\u95ed\u5305\u662f\u6307\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u521b\u5efa\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u8fd9\u4e2a\u51fd\u6570\u7684\u60c5\u51b5\u3002\u5728\u95ed\u5305\u5185\u90e8\u7684\u51fd\u6570\u4e2d\uff0cthis\u7684\u6307\u5411\u901a\u5e38\u4f1a\u4e22\u5931\u6216\u53d8\u4e3a\u5168\u5c40\u5bf9\u8c61\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u6765\u4fdd\u5b58this\u7684\u503c\uff0c\u5e76\u5728\u95ed\u5305\u5185\u90e8\u5f15\u7528\u8fd9\u4e2a\u53d8\u91cf\uff0c\u4ee5\u786e\u4fdd\u5728\u95ed\u5305\u5185\u90e8\u6b63\u786e\u5730\u8bbf\u95eethis\u3002\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u6765\u907f\u514dthis\u6307\u5411\u95ee\u9898\uff0c\u56e0\u4e3a\u7bad\u5934\u51fd\u6570\u4f1a\u7ee7\u627f\u5916\u90e8\u4f5c\u7528\u57df\u7684this\u503c\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\uff0cthis\u5173\u952e\u5b57\u662f\u4e00\u4e2a\u52a8\u6001\u7ed1\u5b9a\u7684\u6982\u5ff5\uff0c\u5b83\u7684\u6307\u5411\u4f9d\u8d56\u4e8e\u51fd\u6570\u7684\u8c03\u7528\u65b9\u5f0f\u3002\u5927\u81f4\u6765\u8bf4\uff0cthis [&hellip;]","protected":false},"author":3,"featured_media":88181,"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\/88179"}],"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=88179"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/88179\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/88181"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=88179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=88179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=88179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}