1. \u901a\u8fc7\u51fd\u6570\u7684 call \u65b9\u6cd5\u8bbe\u7f6e<\/strong><\/span><\/div>\n\u4f60\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u7684call\u65b9\u6cd5\u6765\u8bbe\u7f6e\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u7684 this \u6307\u5411\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6211\u4eec\u5c31\u5e76\u6ca1\u6709\u76f4\u63a5\u8c03\u7528 foo \u51fd\u6570\uff0c\u800c\u662f\u8c03\u7528\u4e86 foo \u7684 call \u65b9\u6cd5\uff0c\u5e76\u5c06 bar \u5bf9\u8c61\u4f5c\u4e3a call \u65b9\u6cd5\u7684\u53c2\u6570\u3002<\/p>\n
let bar = {\r\n myName: \" name1 \",\r\n test1: 1,\r\n};\r\nfunction foo() {\r\n this.myName = \" name2 \";\r\n}\r\nfoo.call(bar);\r\nconsole.log(bar);\r\nconsole.log(myName);<\/pre>\n\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u7136\u540e\u89c2\u5bdf\u8f93\u51fa\u7ed3\u679c\uff0c\u4f60\u5c31\u80fd\u53d1\u73b0 foo \u51fd\u6570\u5185\u90e8\u7684 this \u5df2\u7ecf\u6307\u5411\u4e86 bar \u5bf9\u8c61\uff0c\u56e0\u4e3a\u901a\u8fc7\u6253\u5370 bar \u5bf9\u8c61\uff0c\u53ef\u4ee5\u770b\u51fa bar \u7684 myName \u5c5e\u6027\u5df2\u7ecf\u7531\u201cname1\u201d\u53d8\u4e3a\u201cname2\u201d\u4e86\uff0c\u540c\u65f6\u5728\u5168\u5c40\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u6253\u5370 myName\uff0cJavaScript \u5f15\u64ce\u63d0\u793a\u8be5\u53d8\u91cf\u672a\u5b9a\u4e49\u3002<\/p>\n
\u5176\u5b9e\u9664\u4e86 call \u65b9\u6cd5\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528bind\u548capply\u65b9\u6cd5\u6765\u8bbe\u7f6e\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684 this\uff0c\u4ec5\u4ec5\u662f\u8bed\u6cd5\u7a0d\u6709\u4e0d\u540c\u3002<\/p>\n
2. \u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5\u8bbe\u7f6e<\/strong><\/span><\/div>\n\u8981\u6539\u53d8\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684 this \u6307\u5411\uff0c\u9664\u4e86\u901a\u8fc7\u51fd\u6570\u7684 call \u65b9\u6cd5\u6765\u5b9e\u73b0\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u7684\u65b9\u5f0f\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n
var myObj = {\r\n name: \" name \",\r\n showThis: function () {\r\n console.log(this);\r\n },\r\n};\r\nmyObj.showThis();<\/pre>\n\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a myObj \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u662f\u7531\u4e00\u4e2a name \u5c5e\u6027\u548c\u4e00\u4e2a showThis \u65b9\u6cd5\u7ec4\u6210\u7684\uff0c\u7136\u540e\u518d\u901a\u8fc7 myObj \u5bf9\u8c61\u6765\u8c03\u7528 showThis \u65b9\u6cd5\u3002\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\uff0c\u6700\u7ec8\u8f93\u51fa\u7684 this \u503c\u662f\u6307\u5411 myObj \u7684\u3002<\/p>\n
\u6240\u4ee5\uff0c\u4f60\u53ef\u4ee5\u5f97\u51fa\u8fd9\u6837\u7684\u7ed3\u8bba\uff1a\u4f7f\u7528\u5bf9\u8c61\u6765\u8c03\u7528\u5176\u5185\u90e8\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7684 this \u662f\u6307\u5411\u5bf9\u8c61\u672c\u8eab\u7684\u3002<\/p>\n
\u5176\u5b9e\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8ba4\u4e3a JavaScript \u5f15\u64ce\u5728\u6267\u884cmyObject.showThis()\u65f6\uff0c\u5c06\u5176\u8f6c\u5316\u4e3a\u4e86\uff1a<\/p>\n
myObj.showThis.call(myObj)<\/pre>\n\u63a5\u4e0b\u6765\u6211\u4eec\u7a0d\u5fae\u6539\u53d8\u4e0b\u8c03\u7528\u65b9\u5f0f\uff0c\u628a showThis \u8d4b\u7ed9\u4e00\u4e2a\u5168\u5c40\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u8c03\u7528\u8be5\u5bf9\u8c61\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
var myObj = {\r\n name: \" time \",\r\n showThis: function () {\r\n this.name = \" bang \";\r\n console.log(this);\r\n },\r\n};\r\nvar foo = myObj.showThis;\r\nfoo();<\/pre>\n\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4f60\u4f1a\u53d1\u73b0 this \u53c8\u6307\u5411\u4e86\u5168\u5c40 window \u5bf9\u8c61\u3002<\/p>\n
\u6240\u4ee5\u901a\u8fc7\u4ee5\u4e0a\u4e24\u4e2a\u4f8b\u5b50\u7684\u5bf9\u6bd4\uff0c\u4f60\u53ef\u4ee5\u5f97\u51fa\u4e0b\u9762\u8fd9\u6837\u4e24\u4e2a\u7ed3\u8bba\uff1a<\/p>\n
\u5728\u5168\u5c40\u73af\u5883\u4e2d\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u51fd\u6570\u5185\u90e8\u7684 this \u6307\u5411\u7684\u662f\u5168\u5c40\u53d8\u91cf window\u3002<\/li>\n\u901a\u8fc7\u4e00\u4e2a\u5bf9\u8c61\u6765\u8c03\u7528\u5176\u5185\u90e8\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684 this \u6307\u5411\u5bf9\u8c61\u672c\u8eab\u3002<\/li>\n3. \u901a\u8fc7\u6784\u9020\u51fd\u6570\u4e2d\u8bbe\u7f6e<\/strong><\/span><\/div>\n\u4f60\u53ef\u4ee5\u50cf\u8fd9\u6837\u8bbe\u7f6e\u6784\u9020\u51fd\u6570\u4e2d\u7684 this\uff0c\u5982\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n
function CreateObj() {\r\n this.name = \" time \";\r\n}\r\nvar myObj = new CreateObj();<\/pre>\n\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 new \u521b\u5efa\u4e86\u5bf9\u8c61 myObj\uff0c\u90a3\u4f60\u77e5\u9053\u6b64\u65f6\u7684\u6784\u9020\u51fd\u6570 CreateObj \u4e2d\u7684 this \u5230\u5e95\u6307\u5411\u4e86\u8c01\u5417\uff1f<\/p>\n
\u5176\u5b9e\uff0c\u5f53\u6267\u884c new CreateObj() \u7684\u65f6\u5019\uff0cJavaScript \u5f15\u64ce\u505a\u4e86\u5982\u4e0b\u56db\u4ef6\u4e8b\uff1a<\/p>\n
\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u5bf9\u8c61 tempObj\uff1b<\/li>\n\u63a5\u7740\u8c03\u7528 CreateObj.call \u65b9\u6cd5\uff0c\u5e76\u5c06 tempObj \u4f5c\u4e3a call \u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u8fd9\u6837\u5f53 CreateObj \u7684\u6267\u884c\u4e0a\u4e0b\u6587\u521b\u5efa\u65f6\uff0c\u5b83\u7684 this \u5c31\u6307\u5411\u4e86 tempObj \u5bf9\u8c61\uff1b<\/li>\n\u7136\u540e\u6267\u884c CreateObj \u51fd\u6570\uff0c\u6b64\u65f6\u7684 CreateObj \u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684 this \u6307\u5411\u4e86 tempObj \u5bf9\u8c61\uff1b<\/li>\n\u6700\u540e\u8fd4\u56de tempObj \u5bf9\u8c61\u3002<\/li>\n\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u901a\u8fc7 new \u5173\u952e\u5b57\u6784\u5efa\u597d\u4e86\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u5e76\u4e14\u6784\u9020\u51fd\u6570\u4e2d\u7684 this \u5176\u5b9e\u5c31\u662f\u65b0\u5bf9\u8c61\u672c\u8eab\u3002<\/p>\n
this \u7684\u8bbe\u8ba1\u7f3a\u9677\u4ee5\u53ca\u5e94\u5bf9\u65b9\u6848<\/strong><\/div>\n\u5c31\u6211\u4e2a\u4eba\u800c\u8a00\uff0cthis \u5e76\u4e0d\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u8bbe\u8ba1\uff0c\u56e0\u4e3a\u5b83\u7684\u5f88\u591a\u4f7f\u7528\u65b9\u6cd5\u90fd\u51b2\u51fb\u4eba\u7684\u76f4\u89c9\uff0c\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u5b58\u5728\u7740\u975e\u5e38\u591a\u7684\u5751\u3002\u4e0b\u9762\u54b1\u4eec\u5c31\u6765\u4e00\u8d77\u770b\u770b\u90a3\u4e9b this \u8bbe\u8ba1\u7f3a\u9677\u3002<\/p>\n
1. \u5d4c\u5957\u51fd\u6570\u4e2d\u7684 this \u4e0d\u4f1a\u4ece\u5916\u5c42\u51fd\u6570\u4e2d\u7ee7\u627f<\/strong><\/span><\/div>\n\u6211\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u4e25\u91cd\u7684\u8bbe\u8ba1\u9519\u8bef\uff0c\u5e76\u5f71\u54cd\u4e86\u5f88\u591a\u5f00\u53d1\u8005\u3002<\/p>\n
\u81f3\u4e8e\u5982\u4f55\u89e3\u51b3\uff1f\u4f60\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf self \u7528\u6765\u4fdd\u5b58 this\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528 ES6 \u4e2d\u7684\u7bad\u5934\u51fd\u6570\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n
2. \u666e\u901a\u51fd\u6570\u4e2d\u7684 this \u9ed8\u8ba4\u6307\u5411\u5168\u5c40\u5bf9\u8c61 window<\/strong><\/span><\/div>\n\u4e0a\u9762\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\u4e86\uff0c\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5176\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684 this \u662f\u9ed8\u8ba4\u6307\u5411\u5168\u5c40\u5bf9\u8c61 window \u7684\u3002<\/p>\n
\u4e0d\u8fc7\u8fd9\u4e2a\u8bbe\u8ba1\u4e5f\u662f\u4e00\u79cd\u7f3a\u9677\uff0c\u56e0\u4e3a\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u5e76\u4e0d\u5e0c\u671b\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684 this \u9ed8\u8ba4\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff0c\u56e0\u4e3a\u8fd9\u6837\u4f1a\u6253\u7834\u6570\u636e\u7684\u8fb9\u754c\uff0c\u9020\u6210\u4e00\u4e9b\u8bef\u64cd\u4f5c\u3002\u5982\u679c\u8981\u8ba9\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684 this \u6307\u5411\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6700\u597d\u7684\u65b9\u5f0f\u662f\u901a\u8fc7 call \u65b9\u6cd5\u6765\u663e\u793a\u8c03\u7528\u3002<\/p>\n
\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e JavaScript \u7684\u201c\u4e25\u683c\u6a21\u5f0f\u201d\u6765\u89e3\u51b3\u3002\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u9ed8\u8ba4\u6267\u884c\u4e00\u4e2a\u51fd\u6570\uff0c\u5176\u51fd\u6570\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684 this \u503c\u662f undefined\uff0c\u8fd9\u5c31\u89e3\u51b3\u4e0a\u9762\u7684\u95ee\u9898\u4e86\u3002<\/p>\n
\u603b\u7ed3<\/strong><\/div>\n\u56de\u987e\u4e0b\u5185\u5bb9\uff1a<\/p>\n
\u9996\u5148\uff0c\u5728\u4f7f\u7528 this \u65f6\uff0c\u4e3a\u4e86\u907f\u5751\uff0c\u4f60\u8981\u8c28\u8bb0\u4ee5\u4e0b\u4e09\u70b9\uff1a<\/p>\n
\u5f53\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u51fd\u6570\u4e2d\u7684 this \u5c31\u662f\u8be5\u5bf9\u8c61\uff1b<\/li>\n\u5f53\u51fd\u6570\u88ab\u6b63\u5e38\u8c03\u7528\u65f6\uff0c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0cthis \u503c\u662f undefined\uff0c\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b this \u6307\u5411\u7684\u662f\u5168\u5c40\u5bf9\u8c61 window\uff1b<\/li>\n\u5d4c\u5957\u51fd\u6570\u4e2d\u7684 this \u4e0d\u4f1a\u7ee7\u627f\u5916\u5c42\u51fd\u6570\u7684 this \u503c\u3002<\/li>\n\u6700\u540e\uff0c\u6211\u4eec\u8fd8\u63d0\u4e86\u4e00\u4e0b\u7bad\u5934\u51fd\u6570\uff0c\u56e0\u4e3a\u7bad\u5934\u51fd\u6570\u6ca1\u6709\u81ea\u5df1\u7684\u6267\u884c\u4e0a\u4e0b\u6587\uff0c\u6240\u4ee5\u7bad\u5934\u51fd\u6570\u7684 this \u5c31\u662f\u5b83\u5916\u5c42\u51fd\u6570\u7684 this\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5173\u4e8e this\uff0c\u6211\u4eec\u5f97\u5148\u4ece\u6267\u884c\u4e0a\u4e0b\u6587\u8bf4\u8d77\u3002\u6211\u4eec\u77e5\u9053\uff1a\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u5305\u542b\u4e86\u53d8\u91cf\u73af\u5883\u3001\u8bcd\u6cd5\u73af\u5883\u3001\u5916\u90e8\u73af\u5883\uff0c\u5f53\u7136\u4e5f\u5305\u62ec […]<\/p>\n","protected":false},"author":1482,"featured_media":254031,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=256285"}],"version-history":[{"count":8,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256285\/revisions"}],"predecessor-version":[{"id":256295,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256285\/revisions\/256295"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/254031"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=256285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=256285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=256285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}