{"id":175,"date":"2026-01-21T09:19:00","date_gmt":"2026-01-21T01:19:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=175"},"modified":"2026-06-04T10:10:20","modified_gmt":"2026-06-04T02:10:20","slug":"js%e4%b8%adthis%e6%8c%87%e5%90%91","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/01\/21\/js%e4%b8%adthis%e6%8c%87%e5%90%91\/","title":{"rendered":"JS\u4e2dthis\u6307\u5411"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>this<\/em> \u5173\u952e\u5b57\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u8bed\u6cd5\u70b9\u3002\u6beb\u4e0d\u5938\u5f20\u5730\u8bf4\uff0c\u4e0d\u7406\u89e3\u5b83\u7684\u542b\u4e49\uff0c\u5927\u90e8\u5206\u5f00\u53d1\u4efb\u52a1\u90fd\u65e0\u6cd5\u5b8c\u6210\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>this<\/em> \u53ef\u4ee5\u7528\u5728\u6784\u9020\u51fd\u6570\u4e4b\u4e2d\uff0c\u8868\u793a\u5b9e\u4f8b\u5bf9\u8c61\u3002\u9664\u6b64\u4e4b\u5916\uff0c<em>this<\/em> \u8fd8\u53ef\u4ee5\u7528\u5728\u522b\u7684\u573a\u5408\u3002<strong>\u4f46\u4e0d\u7ba1\u662f\u4ec0\u4e48\u573a\u5408\uff0c<em>this<\/em> \u90fd\u6709\u4e00\u4e2a\u5171\u540c\u70b9\uff1a\u5b83\u603b\u662f\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u4e8e <em>this<\/em> \u7684\u6307\u5411\uff0c\u6709\u4e00\u79cd\u5e7f\u4e3a\u6d41\u4f20\u7684\u8bf4\u6cd5\u5c31\u662f\u201c\u8c01\u8c03\u7528\u5b83\uff0c<em>this<\/em> \u5c31\u6307\u5411\u8c01\u201d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u7684\u8bf4\u6cd5\u6ca1\u6709\u592a\u5927\u7684\u95ee\u9898\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u592a\u5168\u9762\u3002\u603b\u7ed3\u8d77\u6765\uff0c<em>this<\/em> \u7684\u6307\u5411\u89c4\u5f8b\u6709\u5982\u4e0b\u51e0\u6761\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u51fd\u6570\u4f53\u4e2d\uff0c\u975e\u663e\u5f0f\u6216\u9690\u5f0f\u5730\u7b80\u5355\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u51fd\u6570\u5185\u7684 <em>this<\/em> \u4f1a\u88ab\u7ed1\u5b9a\u5230 <em>undefined<\/em> \u4e0a\uff0c\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\u5219\u4f1a\u88ab\u7ed1\u5b9a\u5230\u5168\u5c40\u5bf9\u8c61 <em>window\/global<\/em> \u4e0a\u3002<\/li>\n\n\n\n<li>\u4e00\u822c\u4f7f\u7528 <em>new<\/em> \u65b9\u6cd5\u8c03\u7528\u6784\u9020\u51fd\u6570\u65f6\uff0c\u6784\u9020\u51fd\u6570\u5185\u7684 <em>this<\/em> \u4f1a\u88ab\u7ed1\u5b9a\u5230\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u4e0a\u3002<\/li>\n\n\n\n<li>\u4e00\u822c\u901a\u8fc7 <em>call\/apply\/bind<\/em> \u65b9\u6cd5\u663e\u5f0f\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u4f53\u5185\u7684 <em>this<\/em> \u4f1a\u88ab\u7ed1\u5b9a\u5230\u6307\u5b9a\u53c2\u6570\u7684\u5bf9\u8c61\u4e0a\u3002<\/li>\n\n\n\n<li>\u4e00\u822c\u901a\u8fc7\u4e0a\u4e0b\u6587\u5bf9\u8c61\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u4f53\u5185\u7684 <em>this<\/em> \u4f1a\u88ab\u7ed1\u5b9a\u5230\u8be5\u5bf9\u8c61\u4e0a\u3002<\/li>\n\n\n\n<li>\u5728\u7bad\u5934\u51fd\u6570\u4e2d\uff0c<em>this<\/em> \u7684\u6307\u5411\u662f\u7531\u5916\u5c42\uff08\u51fd\u6570\u6216\u5168\u5c40\uff09\u4f5c\u7528\u57df\u6765\u51b3\u5b9a\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5168\u5c40\u73af\u5883\u4e2d\u7684 <em>this<\/em><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>function f1() {\n    console.log(this);\n}\n\nfunction f2() {\n    'use strict'\n    console.log(this);\n}\n\nf1(); \/\/ window or global\nf2(); \/\/ undefined<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u60c5\u51b5\u76f8\u5bf9\u7b80\u5355\u3001\u76f4\u63a5\uff0c\u51fd\u6570\u5728\u6d4f\u89c8\u5668\u5168\u5c40\u73af\u5883\u4e0b\u88ab\u7b80\u5355\u8c03\u7528\uff0c\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b <em>this<\/em> \u6307\u5411 <em>window<\/em>\uff0c\u5728\u901a\u8fc7 <em>use strict<\/em> \u6307\u660e\u4e25\u683c\u6a21\u5f0f\u7684\u60c5\u51b5\u4e0b\u6307\u5411 <em>undefined<\/em>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const foo = {\n    bar : 10,\n    fn : function(){\n        console.log(this); \/\/ window or global\n        console.log(this.bar); \/\/ undefined\n    }\n}\nvar fn1 = foo.fn;\nfn1();\n\/\/ \u53ea\u662f\u628afn\u51fd\u6570\u5730\u5740\u62ff\u51fa\u6765\uff0cfn1\u548cfoo\u5f7b\u5e95\u89e3\u7ed1\uff0cfn1\u5c31\u662f\u4e00\u4e2a\u666e\u901a\u5168\u5c40\u51fd\u6570<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u7684 <em>this<\/em> \u4ecd\u7136\u6307\u5411 <em>window<\/em>\u3002\u867d\u7136 <em>fn<\/em> \u51fd\u6570\u5728 <em>foo<\/em> \u5bf9\u8c61\u4e2d\u4f5c\u4e3a\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f46\u662f\u5728\u8d4b\u503c\u7ed9 <em>fn1<\/em> \u4e4b\u540e\uff0c<em>fn1<\/em> \u4ecd\u7136\u662f\u5728 <em>window<\/em> \u7684\u5168\u5c40\u73af\u5883\u4e0b\u6267\u884c\u7684\u3002\u56e0\u6b64\u4e0a\u9762\u7684\u4ee3\u7801\u4ecd\u7136\u4f1a\u8f93\u51fa <em>window<\/em> \u548c <em>undefined<\/em>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const foo = {\n    bar : 10,\n    fn : function(){\n        console.log(this); \/\/ { bar: 10, fn: &#91;Function: fn] }\n        console.log(this.bar); \/\/ 10\n    }\n}\nfoo.fn();\n\/\/ this \u2192 foo\u5bf9\u8c61\n\/\/ this.bar \u2192 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u65f6\uff0c<em>this<\/em> \u6307\u5411\u7684\u662f\u6700\u540e\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u5728 <em>foo.fn( )<\/em> \u8bed\u53e5\u4e2d\uff0cthis \u6307\u5411\u7684\u662f <em>foo<\/em> \u5bf9\u8c61\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e0a\u4e0b\u6587\u5bf9\u8c61\u8c03\u7528\u4e2d\u7684 <em>this<\/em><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const student = {\n    name: 'zhangsan',\n    fn: function () {\n        return this;\n    }\n}\nconsole.log(student.fn() === student); \/\/ true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<em>this<\/em> \u6307\u5411\u5f53\u524d\u7684\u5bf9\u8c61 <em>student<\/em>\uff0c\u6240\u4ee5\u6700\u7ec8\u4f1a\u8fd4\u56de <em>true<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u5b58\u5728\u66f4\u590d\u6742\u7684\u8c03\u7528\u5173\u7cfb\u65f6\uff0c\u5982\u4ee5\u4e0b\u4ee3\u7801\u4e2d\u7684\u5d4c\u5957\u5173\u7cfb\uff0c<em>this<\/em> \u5c06\u6307\u5411\u6700\u540e\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982<\/p>\n\n\n\n<ul class=\"wp-block-list\"><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>const student = {\n    name: 'zhangsan',\n    son: {\n        name: 'zhangxiaosan',\n        fn: function () {\n            return this.name\n        }\n    }\n}\nconsole.log(student.son.fn()); \/\/ zhangxiaosan<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<em>this<\/em> \u4f1a\u6307\u5411\u6700\u540e\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64\u8f93\u51fa\u7684\u662f <em>zhangxiaosan<\/em>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const o1 = {\n    text: 'o1',\n    fn: function () {\n        return this.text;\n    }\n}\n\nconst o2 = {\n    text: 'o2',\n    fn: function () {\n        return o1.fn();\n    }\n}\n\nconst o3 = {\n    text: 'o3',\n    fn: function () {\n        var fn = o1.fn;\n        return fn();\n    }\n}\n\nconsole.log(o1.fn()); \/\/ o1\nconsole.log(o2.fn()); \/\/ o1\nconsole.log(o3.fn()); \/\/ undefined<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u5c06 <em>o1.fn<\/em> \u8d4b\u503c\u7ed9\u4e86 <em>fn<\/em>\uff0c\u6240\u4ee5 <em>fn<\/em> \u7b49\u4ef7\u4e8e <em>function () { return this.text; }<\/em>\uff0c\u7136\u540e\u8be5\u51fd\u6570\u5728\u8c03\u7528\u7684\u65f6\u5019\uff0c\u662f\u76f4\u63a5 <em>fn( )<\/em> \u7684\u5f62\u5f0f\u8c03\u7528\u7684\uff0c\u5e76\u4e0d\u662f\u4ee5\u5bf9\u8c61\u7684\u5f62\u5f0f\uff0c\u76f8\u5f53\u4e8e\u8fd8\u662f\u5168\u5c40\u8c03\u7528\uff0c\u6307\u5411 <em>window<\/em>\uff0c\u6240\u4ee5\u6253\u5370\u51fa <em>undefined<\/em>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><em>this<\/em> \u6307\u5411\u7ed1\u5b9a\u4e8b\u4ef6\u7684\u5143\u7d20<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><em>DOM<\/em> \u5143\u7d20\u7ed1\u5b9a\u4e8b\u4ef6\u65f6\uff0c\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u91cc\u9762\u7684 <em>this<\/em> \u6307\u5411\u7ed1\u5b9a\u4e86\u4e8b\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u5730\u65b9\u4e00\u5b9a\u8981\u6ce8\u610f\u5b83\u548c <em>target<\/em> \u7684\u533a\u522b\uff0c<em>target<\/em> \u662f\u6307\u5411\u89e6\u53d1\u4e8b\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ul id=\"color-list\">\n  &lt;li>red&lt;\/li>\n  &lt;li>yellow&lt;\/li>\n  &lt;li>blue&lt;\/li>\n  &lt;li>green&lt;\/li>\n  &lt;li>black&lt;\/li>\n  &lt;li>white&lt;\/li>\n&lt;\/ul><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ this \u662f\u7ed1\u5b9a\u4e8b\u4ef6\u7684\u5143\u7d20\n\/\/ target \u662f\u89e6\u53d1\u4e8b\u4ef6\u7684\u5143\u7d20 \u548c srcElememnt \u7b49\u4ef7\nlet colorList = document.getElementById(\"color-list\");\ncolorList.addEventListener(\"click\", function (event) {\n  console.log('this:', this);\n  console.log('target:', event.target);\n  console.log('srcElement:', event.srcElement);\n})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u6211\u70b9\u51fb\u5982\u4e0b\u4f4d\u7f6e\u65f6\u6253\u5370\u51fa\u6765\u7684\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"275\" src=\"https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/01\/image-1.png\" alt=\"\" class=\"wp-image-177\" srcset=\"https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/01\/image-1.png 716w, https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/01\/image-1-300x115.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e9b\u65f6\u5019\u6211\u4eec\u4f1a\u9047\u5230\u4e00\u4e9b\u56f0\u6270\uff0c\u6bd4\u5982\u5728 <em>div<\/em> \u8282\u70b9\u7684\u4e8b\u4ef6\u51fd\u6570\u5185\u90e8\uff0c\u6709\u4e00\u4e2a\u5c40\u90e8\u7684 <em>callback<\/em> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u88ab\u4f5c\u4e3a\u666e\u901a\u51fd\u6570\u8c03\u7528\u65f6\uff0c<em>callback<\/em> \u5185\u90e8\u7684 <em>this<\/em> \u662f\u6307\u5411\u5168\u5c40\u5bf9\u8c61 <em>window<\/em> \u7684<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"div1\">\u6211\u662f\u4e00\u4e2adiv&lt;\/div><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>window.id = 'window';\ndocument.getElementById('div1').onclick = function(){\n  console.log(this.id); \/\/ div1\n  const callback = function(){\n    console.log(this.id); \/\/ \u56e0\u4e3a\u662f\u666e\u901a\u51fd\u6570\u8c03\u7528\uff0c\u6240\u4ee5 this \u6307\u5411 window\n  }\n  callback();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u65f6\u6709\u4e00\u79cd\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u7528\u4e00\u4e2a\u53d8\u91cf\u4fdd\u5b58 <em>div<\/em> \u8282\u70b9\u7684\u5f15\u7528\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>window.id = 'window';\ndocument.getElementById('div1').onclick = function(){\n  console.log(this.id); \/\/ div1\n  const that = this; \/\/ \u4fdd\u5b58\u5f53\u524d this \u7684\u6307\u5411\n  const callback = function(){\n    console.log(that.id); \/\/ div1\n  }\n  callback();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6539\u53d8 <em>this<\/em> \u6307\u5411<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <em>call\u3001apply\u3001bind<\/em> \u65b9\u6cd5\u4fee\u6539 <em>this<\/em> \u6307\u5411<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e <em>JavaScript<\/em> \u4e2d <em>this<\/em> \u7684\u6307\u5411\u53d7\u51fd\u6570\u8fd0\u884c\u73af\u5883\u7684\u5f71\u54cd\uff0c\u6307\u5411\u7ecf\u5e38\u6539\u53d8\uff0c\u4f7f\u5f97\u5f00\u53d1\u53d8\u5f97\u56f0\u96be\u548c\u6a21\u7cca\uff0c\u6240\u4ee5\u5728\u5c01\u88c5 <em>sdk<\/em> \u6216\u8005\u5199\u4e00\u4e9b\u590d\u6742\u51fd\u6570\u7684\u65f6\u5019\u7ecf\u5e38\u4f1a\u7528\u5230 <em>this<\/em> \u6307\u5411\u7ed1\u5b9a\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u4e0d\u5fc5\u8981\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>call\u3001apply\u3001bind<\/em> \u57fa\u672c\u90fd\u80fd\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0c\u8d77\u5230\u786e\u5b9a <em>this<\/em> \u6307\u5411\u7684\u4f5c\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Function.prototype.call( )<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>call<\/em> \u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a <em>this<\/em> \u7684\u6307\u5411\uff08\u5373\u51fd\u6570\u6267\u884c\u65f6\u6240\u5728\u7684\u7684\u4f5c\u7528\u57df\uff09\uff0c\u7136\u540e\u518d\u6307\u5b9a\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u6267\u884c\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {};\nvar f = function(){\n\treturn this;\n};\nconsole.log(f() === window);  \/\/ this \u6307\u5411 window\nconsole.log(f.call(obj) === obj) \/\/ \u6539\u53d8this \u6307\u5411 obj<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5168\u5c40\u73af\u5883\u8fd0\u884c\u51fd\u6570 <em>f<\/em> \u65f6\uff0c<em>this<\/em> \u6307\u5411\u5168\u5c40\u73af\u5883\uff08\u6d4f\u89c8\u5668\u4e3a <em>window<\/em> \u5bf9\u8c61\uff09\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>call<\/em> \u65b9\u6cd5\u53ef\u4ee5\u6539\u53d8 <em>this<\/em> \u7684\u6307\u5411\uff0c\u6307\u5b9a <em>this<\/em> \u6307\u5411\u5bf9\u8c61 <em>obj<\/em>\uff0c\u7136\u540e\u5728\u5bf9\u8c61 <em>obj<\/em> \u7684\u4f5c\u7528\u57df\u4e2d\u8fd0\u884c\u51fd\u6570 <em>f<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>call<\/em> \u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u5e94\u8be5\u662f\u5bf9\u8c61 <em>obj<\/em>\uff0c\u5982\u679c\u53c2\u6570\u4e3a\u7a7a\u6216 <em>null\u3001undefind<\/em>\uff0c\u5219\u9ed8\u8ba4\u4f20\u53c2\u5168\u5c40\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var n = 123;\nvar obj = { n: 456 };\n\nfunction a() {\n  console.log(this.n);\n}\n\na.call() \/\/ 123\na.call(null) \/\/ 123\na.call(undefined) \/\/ 123\na.call(window) \/\/ 123\na.call(obj) \/\/ 456<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<em>a<\/em> \u51fd\u6570\u4e2d\u7684 <em>this<\/em> \u5173\u952e\u5b57\uff0c\u5982\u679c\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3a <em>123<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f7f\u7528 <em>call<\/em> \u65b9\u6cd5\u5c06 <em>this<\/em> \u5173\u952e\u5b57\u6307\u5411 <em>obj<\/em> \u5bf9\u8c61\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3a <em>456<\/em>\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c <em>call<\/em> \u65b9\u6cd5\u6ca1\u6709\u53c2\u6570\uff0c\u6216\u8005\u53c2\u6570\u4e3a <em>null<\/em> \u6216 <em>undefined<\/em>\uff0c\u5219\u7b49\u540c\u4e8e\u6307\u5411\u5168\u5c40\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <em>call<\/em> \u4f20\u53c2\u4e0d\u662f\u4ee5\u4e0a\u7c7b\u578b\uff0c\u5219\u8f6c\u5316\u6210\u5bf9\u5e94\u7684\u5305\u88c5\u5bf9\u8c61\uff0c\u7136\u540e\u4f20\u5165\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c<em>5<\/em> \u8f6c\u6210 <em>Number<\/em> \u5b9e\u4f8b\uff0c\u7ed1\u5b9a <em>f<\/em> \u5185\u90e8 <em>this<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var f = function () {\n  return this;\n};\n\nf.call(5); \/\/ Number {&#91;&#91;PrimitiveValue]]: 5}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>call<\/em> \u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f <em>this<\/em> \u6307\u5411\u7684\u5bf9\u8c61\uff0c\u4e4b\u540e\u7684\u662f\u51fd\u6570\u56de\u8c03\u6240\u9700\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function add(a, b) {\n  return a + b;\n}\n\nadd.call(this, 1, 2) \/\/ 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>call<\/em> \u65b9\u6cd5\u7684\u4e00\u4e2a\u5e94\u7528\u662f\u8c03\u7528\u5bf9\u8c61\u7684\u539f\u751f\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {};\nobj.hasOwnProperty('toString') \/\/ false\n\n\/\/ \u8986\u76d6\u6389\u7ee7\u627f\u7684 hasOwnProperty \u65b9\u6cd5\nobj.hasOwnProperty = function () {\n  return true;\n};\nobj.hasOwnProperty('toString') \/\/ true\n\nObject.prototype.hasOwnProperty.call(obj, 'toString') \/\/ false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u4e2d <em>hasOwnProperty<\/em> \u662f <em>obj<\/em> \u7ee7\u627f\u6765\u7684\u65b9\u6cd5\uff0c\u7528\u6765\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u5305\u542b\u81ea\u8eab\u7279\u70b9\uff08\u975e\u7ee7\u627f\uff09\u5c5e\u6027\uff0c\u4f46\u662f <em>hasOwnProperty<\/em> \u5e76\u4e0d\u662f\u4fdd\u7559\u5b57\uff0c\u5982\u679c\u88ab\u5bf9\u8c61\u8986\u76d6\uff0c\u4f1a\u9020\u6210\u7ed3\u679c\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>call<\/em> \u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5b83\u5c06 <em>hasOwnProperty<\/em> \u65b9\u6cd5\u7684\u539f\u59cb\u5b9a\u4e49\u653e\u5230 <em>obj<\/em> \u5bf9\u8c61\u4e0a\u6267\u884c\uff0c\u8fd9\u6837\u65e0\u8bba <em>obj<\/em> \u4e0a\u6709\u6ca1\u6709\u540c\u540d\u65b9\u6cd5\uff0c\u90fd\u4e0d\u4f1a\u5f71\u54cd\u7ed3\u679c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Function.prototype.apply( )<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>apply<\/em> \u548c <em>call<\/em> \u4f5c\u7528\u7c7b\u4f3c\uff0c\u4e5f\u662f\u6539\u53d8 <em>this<\/em> \u6307\u5411\uff0c\u7136\u540e\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u552f\u4e00\u533a\u522b\u662f <em>apply<\/em> \u63a5\u6536\u6570\u7ec4\u4f5c\u4e3a\u51fd\u6570\u6267\u884c\u65f6\u7684\u53c2\u6570\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func.apply(thisValue, &#91;arg1, arg2, ...])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>apply<\/em> \u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e5f\u662f <em>this<\/em> \u6240\u8981\u6307\u5411\u7684\u90a3\u4e2a\u5bf9\u8c61\uff0c\u5982\u679c\u8bbe\u4e3a <em>null<\/em> \u6216 <em>undefined<\/em>\uff0c\u5219\u7b49\u540c\u4e8e\u6307\u5b9a\u5168\u5c40\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u7684\u6240\u6709\u6210\u5458\u4f9d\u6b21\u4f5c\u4e3a\u53c2\u6570\uff0c\u4f20\u5165\u539f\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u51fd\u6570\u7684\u53c2\u6570\uff0c\u5728 <em>call<\/em> \u65b9\u6cd5\u4e2d\u5fc5\u987b\u4e00\u4e2a\u4e2a\u6dfb\u52a0\uff0c\u4f46\u662f\u5728 <em>apply<\/em> \u65b9\u6cd5\u4e2d\uff0c\u5fc5\u987b\u4ee5\u6570\u7ec4\u5f62\u5f0f\u6dfb\u52a0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function f(x, y){\n  console.log(x + y);\n}\n\nf.call(null, 1, 1) \/\/ 2\nf.apply(null, &#91;1, 1]) \/\/ 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u7a7a\u503c\uff0c\u8f6c\u5316\u6210 <em>undefined<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 <em>apply<\/em> \u65b9\u6cd5\uff0c\u5229\u7528 <em>Array<\/em> \u6784\u9020\u51fd\u6570\u5c06\u6570\u7ec4\u7684\u7a7a\u5143\u7d20\u53d8\u6210 <em>undefined<\/em>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var a = &#91;'a',,'b'];\nArray.apply(null,a) \/\/&#91;'a',undefind,'b']<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a7a\u5143\u7d20\u4e0e <em>undefined<\/em> \u7684\u5dee\u522b\u5728\u4e8e\uff0c\u6570\u7ec4\u7684 <em>forEach<\/em> \u65b9\u6cd5\u4f1a\u8df3\u8fc7\u7a7a\u5143\u7d20\uff0c\u4f46\u662f\u4e0d\u4f1a\u8df3\u8fc7 <em>undefined<\/em>\u3002\u56e0\u6b64\uff0c\u904d\u5386\u5185\u90e8\u5143\u7d20\u7684\u65f6\u5019\uff0c\u4f1a\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var a = &#91;'a', , 'b'];\n\nfunction print(i) {\n  console.log(i);\n}\n\na.forEach(print)\n\/\/ a\n\/\/ b\n\nArray.apply(null, a).forEach(print)\n\/\/ a\n\/\/ undefined\n\/\/ b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Function.prototype.bind( )<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>bind<\/em> \u7528\u4e8e\u5c06\u51fd\u6570\u4f53\u5185\u7684 <em>this<\/em> \u7ed1\u5b9a\u5230\u67d0\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var d = new Date();\nd.getTime() \/\/ 1481869925657\n\nvar print = d.getTime;\nprint() \/\/ Uncaught TypeError: this is not a Date object.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u62a5\u9519\u662f\u56e0\u4e3a <em>d.getTime<\/em> \u8d4b\u503c\u7ed9 <em>print<\/em> \u540e\uff0c<em>getTime<\/em> \u5185\u90e8\u7684 <em>this<\/em> \u6307\u5411\u65b9\u5f0f\u53d8\u5316\uff0c\u5df2\u7ecf\u4e0d\u518d\u6307\u5411 <em>date<\/em> \u5bf9\u8c61\u5b9e\u4f8b\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var print = d.getTime.bind(d);\nprint() \/\/ 1481869925657<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>bind<\/em> \u63a5\u6536\u7684\u53c2\u6570\u5c31\u662f\u6240\u8981\u7ed1\u5b9a\u7684\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var counter = {\n  count: 0,\n  inc: function () {\n    this.count++;\n  }\n};\n\nvar func = counter.inc.bind(counter);\nfunc();\ncounter.count \/\/ 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>bind<\/em> \u8fd8\u53ef\u4ee5\u63a5\u6536\u66f4\u591a\u7684\u53c2\u6570\uff0c\u5c06\u8fd9\u4e9b\u53c2\u6570\u7ed1\u5b9a\u5230\u539f\u51fd\u6570\u7684\u53c2\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var add = function (x, y) {\n  return x * this.m + y * this.n;\n}\n\nvar obj = {\n  m: 2,\n  n: 2\n};\n\nvar newAdd = add.bind(obj, 5);\nnewAdd(5) \/\/ 20<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<em>bind<\/em> \u65b9\u6cd5\u9664\u4e86\u7ed1\u5b9a <em>this<\/em> \u5bf9\u8c61\uff0c\u8fd8\u5c06 <em>add<\/em> \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570 <em>x<\/em> \u7ed1\u5b9a\u6210 <em>5<\/em>\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570 <em>newAdd<\/em>\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ea\u8981\u518d\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 <em>y<\/em> \u5c31\u80fd\u8fd0\u884c\u4e86\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><em>bind<\/em> \u65b9\u6cd5\u6709\u4e00\u4e9b\u4f7f\u7528\u6ce8\u610f\u70b9\u3002<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\uff081\uff09\u6bcf\u4e00\u6b21\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>bind<\/em> \u65b9\u6cd5\u6bcf\u8fd0\u884c\u4e00\u6b21\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u8fd9\u4f1a\u4ea7\u751f\u4e00\u4e9b\u95ee\u9898\u3002\u6bd4\u5982\uff0c\u76d1\u542c\u4e8b\u4ef6\u7684\u65f6\u5019\uff0c\u4e0d\u80fd\u5199\u6210\u4e0b\u9762\u8fd9\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>element.addEventListener('click', o.m.bind(o));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<em>click<\/em> \u4e8b\u4ef6\u7ed1\u5b9a <em>bind<\/em> \u65b9\u6cd5\u751f\u6210\u7684\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u3002\u8fd9\u6837\u4f1a\u5bfc\u81f4\u65e0\u6cd5\u53d6\u6d88\u7ed1\u5b9a\uff0c\u6240\u4ee5\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u65e0\u6548\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>element.removeEventListener('click', o.m.bind(o));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u5199\u6210\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var listener = o.m.bind(o);\nelement.addEventListener('click', listener);\n\/\/  ...\nelement.removeEventListener('click', listener);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\uff082\uff09\u7ed3\u5408\u56de\u8c03\u51fd\u6570\u4f7f\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56de\u8c03\u51fd\u6570\u662f <em>JavaScript<\/em> \u6700\u5e38\u7528\u7684\u6a21\u5f0f\u4e4b\u4e00\uff0c\u4f46\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u662f\uff0c\u5c06\u5305\u542b <em>this<\/em> \u7684\u65b9\u6cd5\u76f4\u63a5\u5f53\u4f5c\u56de\u8c03\u51fd\u6570\u3002\u89e3\u51b3\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528 <em>bind<\/em> \u65b9\u6cd5\uff0c\u5c06 <em>counter.inc<\/em> \u7ed1\u5b9a <em>counter<\/em>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var counter = {\n  count: 0,\n  inc: function () {\n    'use strict';\n    this.count++;\n  }\n};\n\nfunction callIt(callback) {\n  callback();\n}\n\ncallIt(counter.inc.bind(counter));\ncounter.count \/\/ 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<em>callIt<\/em> \u65b9\u6cd5\u4f1a\u8c03\u7528\u56de\u8c03\u51fd\u6570\u3002\u8fd9\u65f6\u5982\u679c\u76f4\u63a5\u628a <em>counter.inc<\/em> \u4f20\u5165\uff0c\u8c03\u7528\u65f6 <em>counter.inc<\/em> \u5185\u90e8\u7684 <em>this<\/em> \u5c31\u4f1a\u6307\u5411\u5168\u5c40\u5bf9\u8c61\u3002\u4f7f\u7528 <em>bind<\/em> \u65b9\u6cd5\u5c06 <em>counter.inc<\/em> \u7ed1\u5b9a <em>counter<\/em> \u4ee5\u540e\uff0c\u5c31\u4e0d\u4f1a\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c<em>this<\/em> \u603b\u662f\u6307\u5411 <em>counter<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u6709\u4e00\u79cd\u60c5\u51b5\u6bd4\u8f83\u9690\u853d\uff0c\u5c31\u662f\u67d0\u4e9b\u6570\u7ec4\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u5f53\u4f5c\u53c2\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u5185\u90e8\u7684 <em>this<\/em> \u6307\u5411\uff0c\u5f88\u53ef\u80fd\u4e5f\u4f1a\u51fa\u9519\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {\n  name: '\u5f20\u4e09',\n  times: &#91;1, 2, 3],\n  print: function () {\n    this.times.forEach(function (n) {\n      console.log(this.name);\n    });\n  }\n};\n\nobj.print()\n\/\/ \u6ca1\u6709\u4efb\u4f55\u8f93\u51fa<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<em>obj.print<\/em> \u5185\u90e8 <em>this.times<\/em> \u7684 <em>this<\/em> \u662f\u6307\u5411 <em>obj<\/em> \u7684\uff0c\u8fd9\u4e2a\u6ca1\u6709\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\uff0c<em>forEach<\/em> \u65b9\u6cd5\u7684\u56de\u8c03\u51fd\u6570\u5185\u90e8\u7684 <em>this.name<\/em> \u5374\u662f\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff0c\u5bfc\u81f4\u6ca1\u6709\u529e\u6cd5\u53d6\u5230\u503c\u3002\u7a0d\u5fae\u6539\u52a8\u4e00\u4e0b\uff0c\u5c31\u53ef\u4ee5\u770b\u5f97\u66f4\u6e05\u695a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>obj.print = function () {\n  this.times.forEach(function (n) {\n    console.log(this === window);\n  });\n};\n\nobj.print()\n\/\/ true\n\/\/ true\n\/\/ true<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>obj.print = function () {\n  this.times.forEach(function (n) {\n    console.log(this.name);\n  }.bind(this));\n};\n\nobj.print()\n\/\/ \u5f20\u4e09\n\/\/ \u5f20\u4e09\n\/\/ \u5f20\u4e09<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u7ed3\u5408 <em>call<\/em> \u65b9\u6cd5\u4f7f\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5229\u7528 <em>bind<\/em> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6539\u5199\u4e00\u4e9b <em>JavaScript<\/em> \u539f\u751f\u65b9\u6cd5\u7684\u4f7f\u7528\u5f62\u5f0f\uff0c\u4ee5\u6570\u7ec4\u7684 <em>slice<\/em> \u65b9\u6cd5\u4e3a\u4f8b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 2, 3].slice(0, 1) \/\/ &#91;1]\n\/\/ \u7b49\u540c\u4e8e\nArray.prototype.slice.call(&#91;1, 2, 3], 0, 1) \/\/ &#91;1]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6570\u7ec4\u7684 <em>slice<\/em> \u65b9\u6cd5\u4ece <em>[1, 2, 3]<\/em> \u91cc\u9762\uff0c\u6309\u7167\u6307\u5b9a\u4f4d\u7f6e\u548c\u957f\u5ea6\u5207\u5206\u51fa\u53e6\u4e00\u4e2a\u6570\u7ec4\u3002\u8fd9\u6837\u505a\u7684\u672c\u8d28\u662f\u5728 <em>[1, 2, 3]<\/em> \u4e0a\u9762\u8c03\u7528 <em>Array.prototype.slice<\/em> \u65b9\u6cd5\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528 <em>call<\/em> \u65b9\u6cd5\u8868\u8fbe\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u5f97\u5230\u540c\u6837\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>call<\/em> \u65b9\u6cd5\u5b9e\u8d28\u4e0a\u662f\u8c03\u7528 <em>Function.prototype.call<\/em> \u65b9\u6cd5\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528 <em>bind<\/em> \u65b9\u6cd5\u6539\u5199\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var slice = Function.prototype.call.bind(Array.prototype.slice);\nslice(&#91;1, 2, 3], 0, 1) \/\/ &#91;1]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u7684\u542b\u4e49\u5c31\u662f\uff0c\u5c06 <em>Array.prototype.slice<\/em> \u53d8\u6210 <em>Function.prototype.call<\/em> \u65b9\u6cd5\u6240\u5728\u7684\u5bf9\u8c61\uff0c\u8c03\u7528\u65f6\u5c31\u53d8\u6210\u4e86 <em>Array.prototype.slice.call<\/em>\u3002\u7c7b\u4f3c\u7684\u5199\u6cd5\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u6570\u7ec4\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var push = Function.prototype.call.bind(Array.prototype.push);\nvar pop = Function.prototype.call.bind(Array.prototype.pop);\n\nvar a = &#91;1 ,2 ,3];\npush(a, 4)\na \/\/ &#91;1, 2, 3, 4]\n\npop(a)\na \/\/ &#91;1, 2, 3]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u7bad\u5934\u51fd\u6570\u7684 <em>this<\/em> \u6307\u5411<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u6211\u4eec\u7684 <em>this<\/em> \u662f\u4ee5\u51fd\u6570\u7684\u5f62\u5f0f\u8c03\u7528\u65f6\uff0c<em>this<\/em> \u6307\u5411\u7684\u662f\u5168\u5c40\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u5bf9\u4e8e\u7bad\u5934\u51fd\u6570\u6765\u8bb2\uff0c\u5374\u6bd4\u8f83\u7279\u6b8a\u3002\u7bad\u5934\u51fd\u6570\u7684 <em>this<\/em> \u6307\u5411\u59cb\u7ec8\u4e3a\u5916\u5c42\u7684\u4f5c\u7528\u57df\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u6765\u770b\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c<em>this<\/em> \u7684\u6307\u5411\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const obj = {\n    x : 10,\n    test : function(){\n        console.log(this); \/\/ \u6307\u5411 obj \u5bf9\u8c61\n        console.log(this.x); \/\/ 10\n    }\n}\nobj.test();\n\/\/ { x: 10, test: &#91;Function: test] }\n\/\/ 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u666e\u901a\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c<em>this<\/em> \u6307\u5411\u5f53\u524d\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5c31\u662f <em>obj<\/em> \u8fd9\u4e2a\u5bf9\u8c61\uff0c<em>this.x<\/em> \u7684\u503c\u4e3a <em>10<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u662f\u7bad\u5934\u51fd\u6570\u4ee5\u5bf9\u8c61\u7684\u65b9\u5f0f\u88ab\u8c03\u7528\u7684\u65f6\u5019\u7684 <em>this<\/em> \u7684\u6307\u5411\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x = 20;\nconst obj = {\n    x: 10,\n    test: () => {\n        console.log(this); \/\/ {}\n        console.log(this.x); \/\/ undefined\n    }\n}\nobj.test();\n\/\/ {}\n\/\/ undefined<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u7684\u7ed3\u679c\u548c\u4e0a\u9762\u4e0d\u4e00\u6837\uff0c<em>this<\/em> \u6253\u5370\u51fa\u6765\u4e3a { }\uff0c\u800c <em>this.x<\/em> \u7684\u503c\u4e3a <em>undefined<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u9645\u4e0a\u521a\u624d\u6211\u4eec\u6709\u8bb2\u8fc7\uff0c\u7bad\u5934\u51fd\u6570\u7684 <em>this<\/em> \u6307\u5411\u4e0e\u666e\u901a\u51fd\u6570\u4e0d\u4e00\u6837\uff0c\u5b83\u7684 <em>this<\/em> \u6307\u5411\u59cb\u7ec8\u662f\u6307\u5411\u7684\u5916\u5c42\u4f5c\u7528\u57df\u3002\u6240\u4ee5\u8fd9\u91cc\u7684 <em>this<\/em> \u5b9e\u9645\u4e0a\u662f\u6307\u5411\u7684\u5168\u5c40\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8bc1\u660e\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u5c06\u8fd9\u6bb5\u4ee3\u7801\u653e\u5165\u6d4f\u89c8\u5668\u8fd0\u884c\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u7528 <em>var<\/em> \u6240\u58f0\u660e\u7684\u53d8\u91cf\u4f1a\u6210\u4e3a\u5168\u5c40\u5bf9\u8c61 <em>window<\/em> \u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"245\" src=\"https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/01\/image-2.png\" alt=\"\" class=\"wp-image-178\" srcset=\"https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/01\/image-2.png 732w, https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/01\/image-2-300x100.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6765\u8bc1\u660e\u7bad\u5934\u51fd\u6570\u7684 <em>this<\/em> \u6307\u5411\u59cb\u7ec8\u662f\u6307\u5411\u7684\u5916\u5c42\u4f5c\u7528\u57df\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var name = \"JavaScript\";\nconst obj = {\n    name: \"PHP\",\n    test: function () {\n        const i = function () {\n            console.log(this.name);\n            \/\/ i \u662f\u4ee5\u51fd\u6570\u7684\u5f62\u5f0f\u88ab\u8c03\u7528\u7684\uff0c\u6240\u4ee5 this \u6307\u5411\u5168\u5c40\n            \/\/ \u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\u6253\u5370\u51fa JavaScript\uff0cnode \u91cc\u9762\u4e3a undeifned\n        }\n        i();\n    }\n}\nobj.test(); \/\/ JavaScript<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u6211\u4eec\u5c06 i \u51fd\u6570\u4fee\u6539\u4e3a\u7bad\u5934\u51fd\u6570\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var name = \"JavaScript\";\nconst obj = {\n    name : \"PHP\",\n    test : function(){\n        const i = ()=>{\n            console.log(this.name);\n            \/\/ \u7531\u4e8e i \u4e3a\u4e00\u4e2a\u7bad\u5934\u51fd\u6570\uff0c\u6240\u4ee5 this \u662f\u6307\u5411\u5916\u5c42\u7684\n            \/\/ \u6240\u4ee5 this.name \u5c06\u4f1a\u6253\u5370\u51fa PHP\n        }\n        i();\n    }\n}\nobj.test();\/\/ PHP<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u9700\u8981\u8bf4\u4e00\u70b9\u7684\u5c31\u662f\uff0c\u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Test = (name, age) => {\n    this.name = name;\n    this.age = age;\n};\nconst test = new Test(\"zhangshan\", 18);\n\/\/ TypeError: Test is not a constructor<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>this \u5173\u952e\u5b57\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u8bed\u6cd5\u70b9\u3002\u6beb\u4e0d\u5938\u5f20\u5730..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,11],"tags":[],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-js","category-front"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/js\/\" rel=\"category tag\">JS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a>","tag_info":"\u524d\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":2,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/175\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}