{"id":265608,"date":"2024-05-15T16:18:30","date_gmt":"2024-05-15T08:18:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/265608.html"},"modified":"2024-05-15T16:18:36","modified_gmt":"2024-05-15T08:18:36","slug":"javascript-%e7%bc%96%e7%a8%8b%e4%bb%a3%e7%a0%81%e6%80%8e%e4%b9%88%e6%94%b9%e5%8f%98-this-%e6%8c%87%e5%90%91-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/265608.html","title":{"rendered":"JavaScript \u7f16\u7a0b\u4ee3\u7801\u600e\u4e48\u6539\u53d8 this \u6307\u5411"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27130328\/66009650-9d84-432c-bccc-2ae13b31f840.webp\" alt=\"JavaScript \u7f16\u7a0b\u4ee3\u7801\u600e\u4e48\u6539\u53d8 this \u6307\u5411\" \/><\/p>\n<p><p>\u5728JavaScript\u7f16\u7a0b\u4e2d\uff0c<code>this<\/code>\u5173\u952e\u5b57\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6982\u5ff5\uff0c\u5176\u6307\u5411\u51fd\u6570\u6267\u884c\u65f6\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002\u7136\u800c\uff0c\u5728\u4e0d\u540c\u7684\u60c5\u51b5\u4e0b\uff0c<code>this<\/code>\u7684\u6307\u5411\u53ef\u4ee5\u4e0d\u540c\uff0c\u8fd9\u6709\u65f6\u4f1a\u5bfc\u81f4\u4e0d\u7b26\u5408\u9884\u671f\u7684\u884c\u4e3a\u3002\u5e78\u8fd0\u7684\u662f\uff0cJavaScript \u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u6cd5\u6765\u6539\u53d8<code>this<\/code>\u7684\u6307\u5411\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec<strong>\u4f7f\u7528<code>bind()<\/code>\u3001<code>call()<\/code>\u3001<code>apply()<\/code>\u51fd\u6570\uff0c\u4ee5\u53ca\u5229\u7528\u7bad\u5934\u51fd\u6570<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528<code>bind()<\/code><\/strong> \u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u4e14\u76f4\u63a5\u7684\u65b9\u5f0f\u6765\u786e\u4fdd\u51fd\u6570\u4e2d<code>this<\/code>\u5173\u952e\u5b57\u7684\u6307\u5411\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>bind()<\/code>\u51fd\u6570<\/strong> \u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u7684<code>this<\/code>\u88ab\u6c38\u4e45\u7ed1\u5b9a\u5230<code>bind()<\/code>\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u65e0\u8bba\u8fd9\u4e2a\u51fd\u6570\u5982\u4f55\u88ab\u8c03\u7528\u3002\u8fd9\u5bf9\u4e8e\u4e8b\u4ef6\u5904\u7406\u5668\u548c\u56de\u8c03\u51fd\u6570\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u63a7\u5236<code>this<\/code>\u6307\u5411\uff0c\u5373\u4f7f\u5728\u56de\u8c03\u51fd\u6570\u88ab\u5f02\u6b65\u8c03\u7528\u7684\u60c5\u51b5\u4e0b\u3002\u901a\u8fc7<code>bind()<\/code>\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u51fd\u6570\u5185\u90e8\u7684<code>this<\/code>\u6307\u5411\u662f\u6211\u4eec\u671f\u671b\u7684\u5bf9\u8c61\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u5728JavaScript\u5f02\u6b65\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684<code>this<\/code>\u6307\u5411\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>BIND()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>bind()<\/code> \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u5728<code>bind()<\/code>\u88ab\u8c03\u7528\u65f6\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u7684<code>this<\/code>\u88ab\u8bbe\u5b9a\u4e3a<code>bind()<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u5176\u4f59\u53c2\u6570\u5c06\u4f5c\u4e3a\u65b0\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4f9b\u8c03\u7528\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5bf9\u8c61\u548c\u4e00\u4e2a\u60f3\u8981\u5728\u8be5\u5bf9\u8c61\u7684\u4e0a\u4e0b\u6587\u4e2d\u8c03\u7528\u7684\u51fd\u6570\u3002\u4f7f\u7528<code>bind()<\/code>\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u8be5\u51fd\u6570\u5185\u7684<code>this<\/code>\u7ed1\u5b9a\u5230\u6211\u4eec\u6307\u5b9a\u7684\u5bf9\u8c61\u4e0a\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u800c\u4e14\uff0c<code>bind()<\/code>\u65b9\u6cd5\u8fd8\u5141\u8bb8\u6211\u4eec\u9884\u5148\u8bbe\u5b9a\u51fd\u6570\u53c2\u6570\uff0c\u8fd9\u79f0\u4e3a\u90e8\u5206\u5e94\u7528\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u9884\u52a0\u8f7d\u51fd\u6570\u7684\u53c2\u6570\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u5728\u88ab\u8c03\u7528\u65f6\u4f1a\u81ea\u52a8\u5c06\u9884\u8bbe\u7684\u53c2\u6570\u4f5c\u4e3a\u5176\u53c2\u6570\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u5e94\u7528 <code>CALL()<\/code> \u548c <code>APPLY()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>call()<\/code>\u548c<code>apply()<\/code>\u51fd\u6570\u7684\u4f5c\u7528\u662f\u76f8\u4f3c\u7684\uff0c\u5b83\u4eec\u90fd\u662f\u7528\u6765\u6539\u53d8\u51fd\u6570\u8c03\u7528\u65f6\u7684<code>this<\/code>\u6307\u5411\u7684\uff0c\u4f46\u662f\u5728\u53c2\u6570\u7684\u4f20\u9012\u65b9\u5f0f\u4e0a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><code>call()<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u7ed1\u5b9a\u7ed9<code>this<\/code>\u7684\u503c\uff0c\u5176\u4f59\u53c2\u6570\u5c06\u76f4\u63a5\u4f20\u9012\u7ed9\u51fd\u6570\u3002<code>call()<\/code>\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u4efb\u610f\u51fd\u6570\u4e0a\u8bbe\u7f6e<code>this<\/code>\u7684\u503c\uff0c\u5e76\u4e14\u4ee5\u9017\u53f7\u5206\u9694\u7684\u53c2\u6570\u5217\u8868\u5f62\u5f0f\u4f20\u9012\u53c2\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4e0e<code>call()<\/code>\u76f8\u4f3c\uff0c<code>apply()<\/code>\u4e5f\u662f\u7528\u6765\u6539\u53d8\u51fd\u6570\u6267\u884c\u65f6<code>this<\/code>\u7684\u6307\u5411\uff0c\u4f46\u533a\u522b\u5728\u4e8e<code>apply()<\/code>\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5b9a\u4e49\u4e86<code>this<\/code>\u7684\u503c\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53c2\u6570\u7684\u6570\u7ec4\u3002\u8fd9\u5bf9\u4e8e\u51fd\u6570\u4e0d\u786e\u5b9a\u53c2\u6570\u6570\u91cf\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u7b80\u5355\u5730\u4f20\u9012\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u53c2\u6570\u7684\u5177\u4f53\u6570\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5229\u7528 \u7bad\u5934\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u8868\u8fbe\u5f0f\u6709\u4e00\u4e2a\u663e\u8457\u7684\u7279\u70b9\uff1a\u5b83\u6ca1\u6709\u81ea\u5df1\u7684<code>this<\/code>\uff0c\u7bad\u5934\u51fd\u6570\u5728\u88ab\u5b9a\u4e49\u65f6\u6355\u83b7\u5176\u6240\u5728\u4e0a\u4e0b\u6587\u7684<code>this<\/code>\u503c\u4f5c\u4e3a\u81ea\u5df1\u7684<code>this<\/code>\u503c\uff0c\u56e0\u6b64\uff0c\u5b83\u9002\u7528\u4e8e\u90a3\u4e9b\u9700\u8981\u56fa\u5b9a<code>this<\/code>\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u7bad\u5934\u51fd\u6570\u7279\u522b\u9002\u5408\u4e0e\u9ad8\u9636\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u6bd4\u5982<code>map()<\/code>\u3001<code>filter()<\/code>\u548c<code>reduce()<\/code>\uff0c\u56e0\u4e3a\u4f60\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u7ed1\u5b9a<code>this<\/code>\uff0c\u7b80\u5316\u4e86\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u7136\u800c\uff0c\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u65f6\u4e5f\u5e94\u6ce8\u610f\u5176\u5c40\u9650\u6027\uff0c\u4f8b\u5982\uff0c\u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u7528\u4f5c\u6784\u9020\u51fd\u6570\uff0c\u4e5f\u4e0d\u80fd\u4f7f\u7528<code>bind()<\/code>\u3001<code>call()<\/code>\u548c<code>apply()<\/code>\u65b9\u6cd5\u6765\u6539\u53d8<code>this<\/code>\u7684\u6307\u5411\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4e0a\u4e0b\u6587\u5bf9\u8c61 \u4e2d \u4f7f\u7528 <code>THIS<\/code><\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e2d\uff0c\u6b63\u786e\u7406\u89e3\u548c\u4f7f\u7528<code>this<\/code>\u5173\u952e\u5b57\u5bf9\u4e8e\u8bbf\u95ee\u5bf9\u8c61\u81ea\u8eab\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u901a\u8fc7\u5728\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e2d\u4f7f\u7528<code>this<\/code>\uff0c\u53ef\u4ee5\u5f15\u7528\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u5f53\u524d\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5728\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e2d\u4f7f\u7528<code>this<\/code>\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u5373\u4f7f\u5bf9\u8c61\u7684\u540d\u5b57\u53d1\u751f\u53d8\u5316\uff0c\u53ea\u8981\u7ed3\u6784\u4fdd\u6301\u4e0d\u53d8\uff0c\u65b9\u6cd5\u5185\u7684<code>this<\/code>\u5f15\u7528\u4ecd\u80fd\u6b63\u786e\u5730\u6307\u5411\u5f53\u524d\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u6700\u4f73\u5b9e\u8df5\u662f\u5728\u6784\u5efa\u5bf9\u8c61\u548c\u7c7b\u65f6\u660e\u667a\u5730\u4f7f\u7528<code>this<\/code>\u5173\u952e\u5b57\uff0c\u7279\u522b\u662f\u5728\u4e8b\u4ef6\u5904\u7406\u3001\u5f02\u6b65\u7f16\u7a0b\u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u4e2d\uff0c<code>this<\/code>\u53d1\u6325\u7740\u91cd\u8981\u7684\u4f5c\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0cJavaScript\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u6539\u53d8\u51fd\u6570\u4e2d<code>this<\/code>\u7684\u6307\u5411\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u3002\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u7528\u6cd5\u5bf9\u4e8e\u7f16\u5199\u7075\u6d3b\u3001\u53ef\u7ef4\u62a4\u7684JavaScript\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728JavaScript\u4ee3\u7801\u4e2d\u6539\u53d8this\u6307\u5411\uff1f<\/strong><\/p>\n<p>\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u6539\u53d8this\u6307\u5411\uff1a<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>call()<\/code>\u65b9\u6cd5\u6216<code>apply()<\/code>\u65b9\u6cd5\uff1a\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4e34\u65f6\u6539\u53d8\u51fd\u6570\u7684this\u6307\u5411\u3002<code>call()<\/code>\u65b9\u6cd5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u5c06this\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u540e\u9762\u7684\u53c2\u6570\u662f\u51fd\u6570\u7684\u5b9e\u53c2\uff1b<code>apply()<\/code>\u65b9\u6cd5\u4e0e<code>call()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u53ea\u662f\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002<br \/>\n \u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">function sayHello() {\n  console.log(&quot;Hello, &quot; + this.name);\n}\n\nvar person = { name: &quot;John&quot; };\n\nsayHello.call(person); \/\/ \u8f93\u51fa\uff1aHello, John\nsayHello.apply(person); \/\/ \u8f93\u51fa\uff1aHello, John\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528<code>bind()<\/code>\u65b9\u6cd5\uff1a<code>bind()<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5728\u8c03\u7528\u65b0\u51fd\u6570\u65f6\uff0c\u5c06\u539f\u51fd\u6570\u7684this\u6307\u5411\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u5bf9\u8c61\u3002\u4e0e<code>call()<\/code>\u548c<code>apply()<\/code>\u65b9\u6cd5\u4e0d\u540c\u7684\u662f\uff0c<code>bind()<\/code>\u65b9\u6cd5\u4e0d\u4f1a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u3002<br \/>\n \u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">function sayHello() {\n  console.log(&quot;Hello, &quot; + this.name);\n}\n\nvar person = { name: &quot;John&quot; };\nvar sayHelloToPerson = sayHello.bind(person);\n\nsayHelloToPerson(); \/\/ \u8f93\u51fa\uff1aHello, John\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff1a\u7bad\u5934\u51fd\u6570\u4e0d\u81ea\u52a8\u7ed1\u5b9athis\uff0c\u800c\u662f\u7ee7\u627f\u5916\u5c42\u4f5c\u7528\u57df\u7684this\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u7b80\u5355\u5730\u6539\u53d8this\u6307\u5411\u3002<br \/>\n \u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">var person = {\n  name: &quot;John&quot;,\n  sayHello: () =&gt; {\n    console.log(&quot;Hello, &quot; + this.name);\n  }\n};\n\nperson.sayHello(); \/\/ \u8f93\u51fa\uff1aHello, undefined\uff08\u7bad\u5934\u51fd\u6570\u4e2d\u7684this\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff09\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><strong>2. \u5982\u679c\u60f3\u5728JavaScript\u4ee3\u7801\u4e2d\u5b9e\u73b0\u51fd\u6570\u4e2d\u7684this\u6307\u5411\u52a8\u6001\u6539\u53d8\u600e\u4e48\u529e\uff1f<\/strong><\/p>\n<p>\u5728JavaScript\u4e2d\uff0c\u5982\u679c\u60f3\u5728\u51fd\u6570\u4e2d\u52a8\u6001\u6539\u53d8this\u6307\u5411\uff0c\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53d8\u91cf\u4f20\u9012\uff0c\u5e76\u5728\u95ed\u5305\u4e2d\u4f7f\u7528<code>call()<\/code>\u6216<code>apply()<\/code>\u65b9\u6cd5\u6765\u6539\u53d8\u51fd\u6570\u7684this\u6307\u5411\u3002<br \/>\n\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">function sayHello() {\n  console.log(&quot;Hello, &quot; + this.name);\n}\n\nfunction changeThis(func, obj) {\n  return function() {\n    func.call(obj);\n  };\n}\n\nvar person1 = { name: &quot;John&quot; };\nvar person2 = { name: &quot;Alice&quot; };\n\nvar sayHelloToPerson1 = changeThis(sayHello, person1);\nvar sayHelloToPerson2 = changeThis(sayHello, person2);\n\nsayHelloToPerson1(); \/\/ \u8f93\u51fa\uff1aHello, John\nsayHelloToPerson2(); \/\/ \u8f93\u51fa\uff1aHello, Alice\n<\/code><\/pre>\n<p><strong>3. \u5982\u4f55\u5728JavaScript\u4ee3\u7801\u4e2d\u907f\u514dthis\u6307\u5411\u7684\u6df7\u4e71\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514dJavaScript\u4ee3\u7801\u4e2dthis\u6307\u5411\u7684\u6df7\u4e71\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528\u4e25\u683c\u6a21\u5f0f\uff08strict mode\uff09\uff1a\u5728\u51fd\u6570\u6216\u4ee3\u7801\u5757\u4e2d\u6dfb\u52a0&quot;use strict&quot;\uff0c\u4ee5\u542f\u7528\u4e25\u683c\u6a21\u5f0f\u3002\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u4e0d\u5141\u8bb8\u9ed8\u8ba4\u7ed1\u5b9a\uff0cthis\u5c06\u4e3aundefined\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u610f\u5916\u7684\u5168\u5c40\u53d8\u91cf\u6c61\u67d3\u548cthis\u6307\u5411\u9519\u8bef\u3002<br \/>\n \u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">&quot;use strict&quot;;\n\nfunction sayHello() {\n  console.log(&quot;Hello, &quot; + this.name);\n}\n\nvar person = { name: &quot;John&quot; };\n\nsayHello.call(person); \/\/ \u8f93\u51fa\uff1aTypeError: Cannot read property &#039;name&#039; of undefined\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff1a\u5982\u524d\u6240\u8ff0\uff0c\u7bad\u5934\u51fd\u6570\u4e0d\u4f1a\u81ea\u52a8\u7ed1\u5b9athis\uff0c\u800c\u662f\u7ee7\u627f\u5916\u5c42\u4f5c\u7528\u57df\u7684this\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u907f\u514dthis\u6307\u5411\u7684\u6df7\u4e71\u3002<br \/>\n \u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">var person = {\n  name: &quot;John&quot;,\n  sayHello: function() {\n    setTimeout(() =&gt; {\n      console.log(&quot;Hello, &quot; + this.name);\n    }, 1000);\n  }\n};\n\nperson.sayHello(); \/\/ \u8f93\u51fa\uff1aHello, John\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528bind()\u65b9\u6cd5\u6216\u95ed\u5305\uff1a\u5982\u524d\u9762\u6240\u8ff0\uff0c\u53ef\u4ee5\u4f7f\u7528bind()\u65b9\u6cd5\u6216\u95ed\u5305\u6765\u5728\u51fd\u6570\u8c03\u7528\u65f6\u52a8\u6001\u5730\u6539\u53d8this\u6307\u5411\uff0c\u4ece\u800c\u907f\u514dthis\u6307\u5411\u7684\u6df7\u4e71\u3002<br \/>\n \u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">function sayHello() {\n  console.log(&quot;Hello, &quot; + this.name);\n}\n\nvar person = { name: &quot;John&quot; };\n\nvar sayHelloToPerson = sayHello.bind(person);\n\nsayHelloToPerson(); \/\/ \u8f93\u51fa\uff1aHello, John\n<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u7f16\u7a0b\u4e2d\uff0cthis\u5173\u952e\u5b57\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6982\u5ff5\uff0c\u5176\u6307\u5411\u51fd\u6570\u6267\u884c\u65f6\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002\u7136\u800c\uff0c\u5728\u4e0d\u540c\u7684\u60c5\u51b5 [&hellip;]","protected":false},"author":3,"featured_media":265618,"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\/265608"}],"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=265608"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/265608\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/265618"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=265608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=265608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=265608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}