{"id":166042,"date":"2024-05-08T15:28:30","date_gmt":"2024-05-08T07:28:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/166042.html"},"modified":"2024-05-08T15:28:34","modified_gmt":"2024-05-08T07:28:34","slug":"javascript%e4%b8%ad%e7%9a%84this%e6%98%af-this-page-%e7%9a%84%e6%84%8f%e6%80%9d%e5%90%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/166042.html","title":{"rendered":"JavaScript\u4e2d\u7684\\this\\\u662f  this page \u7684\u610f\u601d\u5417"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233105\/85f96bdc-e8f7-4931-89ab-1293b66dd368.webp\" alt=\"JavaScript\u4e2d\u7684\\this\\\u662f  this page \u7684\u610f\u601d\u5417\" \/><\/p>\n<p><p><strong>\u4e0d\u662f\uff0cJavaScript\u4e2d\u7684<code>this<\/code>\u5173\u952e\u5b57\u5e76\u4e0d\u8868\u793a\u201cthis page\u201d\uff0c\u800c\u662f\u6307\u5411\u51fd\u6570\u6b63\u5728\u6267\u884c\u7684\u4e0a\u4e0b\u6587\u3002<\/strong> \u5728\u4e0d\u540c\u7684\u60c5\u51b5\u4e0b\uff0c<code>this<\/code>\u53ef\u4ee5\u6307\u5411\u4e0d\u540c\u7684\u5bf9\u8c61\uff1a\u5728\u5168\u5c40\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\uff0c<code>this<\/code>\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff1b\u5728\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e2d\uff0c<code>this<\/code>\u6307\u5411\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u5bf9\u8c61\uff1b\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\uff0c<code>this<\/code>\u901a\u5e38\u6307\u5411\u63a5\u6536\u4e8b\u4ef6\u7684\u5143\u7d20\uff1b\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c<code>this<\/code>\u7684\u503c\u4e3a<code>undefined<\/code>\uff0c\u9664\u975e\u88ab\u660e\u786e\u8bbe\u7f6e\uff1b\u5728\u7bad\u5934\u51fd\u6570\u4e2d\uff0c<code>this<\/code>\u88ab\u8bbe\u7f6e\u4e3a\u5b83\u5728\u5b9a\u4e49\u65f6\u7684\u4e0a\u4e0b\u6587\uff0c\u5e76\u4e0d\u4f1a\u968f\u7740\u51fd\u6570\u7684\u8c03\u7528\u800c\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5168\u5c40\u4e0a\u4e0b\u6587\u4e2d\u7684 <code>this<\/code><\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4ee3\u7801\u7684\u6700\u9876\u5c42\uff0c\u4e5f\u5373\u975e\u4efb\u4f55\u51fd\u6570\u5185\u90e8\u7684\u4f4d\u7f6e\uff0c\u7528<code>this<\/code>\u4f1a\u6307\u5411\u5168\u5c40\u5bf9\u8c61\u3002\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u5168\u5c40\u5bf9\u8c61\u901a\u5e38\u662f<code>window<\/code>\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5728\u5168\u5c40\u4e0a\u4e0b\u6587\u4e2d\uff0c<code>this<\/code>\u6709\u65f6\u53ef\u4ee5\u4e0e<code>window<\/code>\u4e92\u6362\u4f7f\u7528\u3002\u4f46\u5728Node.js\u4e2d\uff0c\u5168\u5c40\u5bf9\u8c61\u5e76\u4e0d\u662f<code>window<\/code>\uff0c\u800c\u662f<code>global<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">console.log(this === window); \/\/ \u5728\u6d4f\u89c8\u5668\u4e2d\u8fd4\u56detrue<\/p>\n<p>var a = 37;<\/p>\n<p>console.log(window.a); \/\/ \u8f93\u51fa37<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u4e0a\u4e0b\u6587\u4e2d\u7684 <code>this<\/code><\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5185\u90e8\uff0c<code>this<\/code>\u7684\u503c\u53d6\u51b3\u4e8e\u51fd\u6570\u7684\u8c03\u7528\u65b9\u5f0f\u3002\u5982\u679c\u662f\u7b80\u5355\u8c03\u7528\uff0c\u5373\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u800c\u4e0d\u662f\u4f5c\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u6216\u901a\u8fc7<code>new<\/code>\u5173\u952e\u5b57\u8c03\u7528\uff0c\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b<code>this<\/code>\u4f1a\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff0c\u800c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c<code>this<\/code>\u4f1a\u662f<code>undefined<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function f1(){<\/p>\n<p>  return this;<\/p>\n<p>}<\/p>\n<p>\/\/ \u5728\u6d4f\u89c8\u5668\u7684\u975e\u4e25\u683c\u6a21\u5f0f\u4e2d\uff0c\u8fd9\u5c06\u8fd4\u56de\u5168\u5c40\u5bf9\u8c61<\/p>\n<p>f1() === window; \/\/ true<\/p>\n<p>\/\/ \u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u5219\u8fd4\u56deundefined<\/p>\n<p>function f2(){<\/p>\n<p>  &#039;use strict&#039;;<\/p>\n<p>  return this;<\/p>\n<p>}<\/p>\n<p>f2() === undefined; \/\/ true<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5bf9\u8c61\u65b9\u6cd5\u4e2d\u7684 <code>this<\/code><\/h3>\n<\/p>\n<p><p>\u5f53\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u91cc\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c<code>this<\/code>\u5c06\u6307\u5411\u90a3\u4e2a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var obj = {<\/p>\n<p>  prop: 37,<\/p>\n<p>  f: function() {<\/p>\n<p>    return this.prop;<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p>console.log(obj.f()); \/\/ \u8f93\u51fa37<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc<code>f<\/code>\u4f5c\u4e3a<code>obj<\/code>\u7684\u4e00\u4e2a\u5c5e\u6027\u88ab\u8c03\u7528\uff0c\u56e0\u6b64\u51fd\u6570\u5185\u7684<code>this<\/code>\u6307\u5411<code>obj<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6784\u9020\u51fd\u6570\u4e2d\u7684 <code>this<\/code><\/h3>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u5185\u90e8\u7684<code>this<\/code>\u6307\u7684\u662f\u6b63\u5728\u88ab\u521b\u5efa\u7684\u65b0\u5bf9\u8c61\u3002\u5f53\u4f60\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u8be5\u51fd\u6570\u5c31\u4f1a\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u53bb\u521b\u5efa\u4e00\u4e2a\u65b0\u5b9e\u4f8b\uff0c<code>this<\/code>\u5c31\u4f1a\u7ed1\u5b9a\u5230\u8fd9\u4e2a\u65b0\u5b9e\u4f8b\u4e0a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Gadget(name, color) {<\/p>\n<p>  this.name = name;<\/p>\n<p>  this.color = color;<\/p>\n<p>  this.whatAreYou = function(){<\/p>\n<p>    return &#039;I am a &#039; + this.color + &#039; &#039; + this.name;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>var newGad = new Gadget(&#039;iPhone&#039;, &#039;Silver&#039;);<\/p>\n<p>console.log(newGad.whatAreYou()); \/\/ \u8f93\u51fa&quot;I am a Silver iPhone&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed1\u5b9a <code>this<\/code><\/h3>\n<\/p>\n<p><p>JavaScript \u63d0\u4f9b\u4e86 <code>.bind()<\/code>\u3001<code>.call()<\/code>\u3001\u548c <code>.apply()<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u51fd\u6570\u7684<code>this<\/code>\u503c\uff0c\u65e0\u8bba\u51fd\u6570\u5982\u4f55\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<ul>\n<li><code>.bind()<\/code> \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u4f20\u5165\u53c2\u6570\u5217\u8868\u4f5c\u4e3a\u65b0\u51fd\u6570\u88ab\u8c03\u7528\u65f6<code>this<\/code>\u7684\u524d\u7f6e\u53c2\u6570\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">function f(){<\/p>\n<p>  return this.a;<\/p>\n<p>}<\/p>\n<p>var g = f.bind({a: &#039;azerty&#039;});<\/p>\n<p>console.log(g()); \/\/ \u8f93\u51fa &#039;azerty&#039;<\/p>\n<p>var o = {a: 37, f: f, g: g};<\/p>\n<p>console.log(o.f(), o.g()); \/\/ \u8f93\u51fa &#039;37, azerty&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>.call()<\/code> \u548c <code>.apply()<\/code> \u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8bbe\u7f6e\u51fd\u6570\u8c03\u7528\u65f6\u7684<code>this<\/code>\u503c\uff0c\u5e76\u4e14\u7acb\u5373\u6267\u884c\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">function add(c, d){<\/p>\n<p>  return this.a + this.b + c + d;<\/p>\n<p>}<\/p>\n<p>var o = {a: 1, b: 3};<\/p>\n<p>add.call(o, 5, 7); \/\/ 1 + 3 + 5 + 7 = 16<\/p>\n<p>add.apply(o, [10, 20]); \/\/ 1 + 3 + 10 + 20 = 34<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7bad\u5934\u51fd\u6570\u4e2d\u7684 <code>this<\/code><\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4e0d\u7ed1\u5b9a\u81ea\u5df1\u7684<code>this<\/code>\uff0c\u5b83\u4eec\u5728\u81ea\u5df1\u7684\u4f5c\u7528\u57df\u4e2d\u7ee7\u627f<code>this<\/code>\u3002\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u7bad\u5934\u51fd\u6570\u4e0e\u666e\u901a\u51fd\u6570\u7684<code>this<\/code>\u884c\u4e3a\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function NormalFunction() {<\/p>\n<p>  this.value = 20;<\/p>\n<p>  setTimeout(function() {<\/p>\n<p>    console.log(this.value); \/\/ \u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\u8f93\u51faundefined\u6216\u5168\u5c40\u5bf9\u8c61\u7684value\u5c5e\u6027\u503c\uff0c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u62a5\u9519<\/p>\n<p>  }, 100);<\/p>\n<p>}<\/p>\n<p>function ArrowFunction() {<\/p>\n<p>  this.value = 20;<\/p>\n<p>  setTimeout(() =&gt; {<\/p>\n<p>    console.log(this.value); \/\/ \u8f93\u51fa20<\/p>\n<p>  }, 100);<\/p>\n<p>}<\/p>\n<p>new NormalFunction();<\/p>\n<p>new ArrowFunction();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u7bad\u5934\u51fd\u6570\u5b9e\u4f8b\u4e2d\uff0c<code>setTimeout<\/code>\u91cc\u7684\u51fd\u6570\u7ee7\u627f\u4e86\u5916\u56f4\u4f5c\u7528\u57df<code>ArrowFunction<\/code>\u4e2d\u7684<code>this<\/code>\u3002\u4e0e\u4e4b\u76f8\u53cd\uff0c\u5728\u4f20\u7edf\u51fd\u6570\u4e2d\uff0c<code>setTimeout<\/code>\u4e2d\u7684\u51fd\u6570\u5185\u90e8\u7684<code>this<\/code>\u503c\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff0c\u6216\u8005\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u662f<code>undefined<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u603b\u800c\u8a00\u4e4b\uff0c<code>this<\/code>\u5728JavaScript\u4e2d\u662f\u4e00\u4e2a\u590d\u6742\u7684\u6982\u5ff5\uff0c\u5176\u6307\u5411\u4f9d\u8d56\u4e8e\u51fd\u6570\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\u3002\u6b63\u786e\u7406\u89e3\u548c\u8fd0\u7528<code>this<\/code>\u53ef\u4ee5\u5e2e\u4f60\u907f\u514d\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5e76\u66f4\u6709\u6548\u5730\u7f16\u5199\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5728JavaScript\u4e2d\uff0cthis\u5173\u952e\u5b57\u8868\u793a\u4ec0\u4e48\uff1f<\/strong><br \/>\nthis\u5173\u952e\u5b57\u5728JavaScript\u4e2d\u7528\u4e8e\u6307\u4ee3\u5f53\u524d\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684\u5bf9\u8c61\u3002\u5b83\u53ef\u4ee5\u6839\u636e\u5f53\u524d\u7684\u8c03\u7528\u73af\u5883\u800c\u53d8\u5316\uff0c\u53ef\u80fd\u6307\u5411\u4e0d\u540c\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5728\u51fd\u6570\u4e2d\uff0cthis\u901a\u5e38\u6307\u5411\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u7684\u5bf9\u8c61\uff0c\u800c\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0cthis\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff08\u901a\u5e38\u662f\u6d4f\u89c8\u5668\u4e2d\u7684window\u5bf9\u8c61\uff09\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u786e\u4fdd\u5728JavaScript\u4e2d\u6b63\u786e\u4f7f\u7528this\u5173\u952e\u5b57\uff1f<\/strong><br \/>\n\u4e3a\u4e86\u786e\u4fdd\u6b63\u786e\u4f7f\u7528this\u5173\u952e\u5b57\uff0c\u9700\u8981\u4e86\u89e3\u5b83\u5728\u4e0d\u540c\u4e0a\u4e0b\u6587\u4e2d\u7684\u6307\u5411\u3002\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u89c4\u5219\u548c\u6280\u5de7\u6765\u786e\u5b9athis\u7684\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528call\u3001apply\u6216bind\u65b9\u6cd5\u663e\u5f0f\u5730\u7ed1\u5b9athis\u7684\u503c\uff0c\u6216\u8005\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u6765\u7ee7\u627f\u5916\u90e8\u4f5c\u7528\u57df\u7684this\u503c\u3002\u6b64\u5916\uff0c\u4e86\u89e3JavaScript\u4e2d\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u89c4\u5219\u4e5f\u5f88\u91cd\u8981\uff0c\u4ee5\u4fbf\u6b63\u786e\u7406\u89e3this\u5173\u952e\u5b57\u7684\u6307\u5411\u3002<\/p>\n<p><strong>3. JavaScript\u4e2d\u7684this\u4e0ethis page\u6709\u4f55\u533a\u522b\uff1f<\/strong><br \/>\nJavaScript\u4e2d\u7684this\u4e0ethis page\u5e76\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u3002this page\u901a\u5e38\u6307\u7684\u662f\u5f53\u524d\u6b63\u5728\u6d4f\u89c8\u7684\u7f51\u9875\u6216\u9875\u9762\u3002\u800cJavaScript\u4e2d\u7684this\u5173\u952e\u5b57\u662f\u7528\u4e8e\u6307\u4ee3\u5f53\u524d\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684\u5bf9\u8c61\u3002\u5b83\u7684\u6307\u5411\u662f\u52a8\u6001\u7684\uff0c\u53d6\u51b3\u4e8e\u8c03\u7528\u51fd\u6570\u7684\u65b9\u5f0f\u548c\u4f4d\u7f6e\u3002\u56e0\u6b64\uff0cthis\u4e0ethis page\u662f\u4e0d\u540c\u7684\u6982\u5ff5\uff0c\u4e0d\u80fd\u7b80\u5355\u5730\u5c06\u5b83\u4eec\u7b49\u540c\u8d77\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e0d\u662f\uff0cJavaScript\u4e2d\u7684this\u5173\u952e\u5b57\u5e76\u4e0d\u8868\u793a\u201cthis page\u201d\uff0c\u800c\u662f\u6307\u5411\u51fd\u6570\u6b63\u5728\u6267\u884c\u7684\u4e0a\u4e0b\u6587\u3002 \u5728 [&hellip;]","protected":false},"author":3,"featured_media":166051,"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\/166042"}],"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=166042"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166042\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/166051"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=166042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=166042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=166042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}