{"id":150,"date":"2022-08-18T21:35:01","date_gmt":"2022-08-18T13:35:01","guid":{"rendered":"http:\/\/weboob.top\/blog\/?p=150"},"modified":"2022-08-18T21:35:01","modified_gmt":"2022-08-18T13:35:01","slug":"javascript%e4%b8%ad%e7%9a%84-this-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/blog.weboob.top\/?p=150","title":{"rendered":"Javascript\u4e2d\u7684\u00a0this\u00a0\u95ee\u9898"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u524d\u5bfc\u77e5\u8bc6&nbsp;<a href=\"https:\/\/baijiahao.baidu.com\/s?id=1728527150329407234&amp;wfr=spider&amp;for=pc\">\u539f\u6587<\/a>&nbsp;|&nbsp;<a href=\"http:\/\/weboob.top\/blog\/?p=148\">\u5907\u4efd<\/a>:<\/p><\/blockquote>\n\n\n\n<ul class=\"wp-block-list\"><li>Javascript\u4e2d\u7684&nbsp;<code>this<\/code>&nbsp;\u5177\u4f53\u662f\u6307\u4ee3\u8c01\uff0c\u8981\u770b&nbsp;<code>\u5f53\u524d\u8fd0\u884c\u73af\u5883<\/code>(&nbsp;<em><strong>\u5b9a\u4e49this<a href=\"https:\/\/www.jianshu.com\/p\/c9d1763b4297\">\u6808\u5e27<\/a>\u7684\u4e0a\u5c42\u6808\u5e27\uff0c\u6709\u5f85\u9a8c\u8bc1<\/strong><\/em>&nbsp;)<\/li><li>\u4e0d\u80fd\u53ea\u770b\u5b9a\u4e49\u90e8\u5206\uff1b\u4e0d\u80fd\u53ea\u770b\u5b9a\u4e49\u90e8\u5206\uff1b\u4e0d\u80fd\u53ea\u770b\u5b9a\u4e49\u90e8\u5206<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u770b\u6709\u5173\u8d44\u6599\u540e\uff0c\u5177\u4f53\u53ef\u80fd\u5206\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u8c03\u7528\u65b9\u5f0f<\/th><th>\u793a\u4f8b<\/th><th>this\u6307\u5411<\/th><\/tr><\/thead><tbody><tr><td>\u901a\u8fc7new\u8c03\u7528<\/td><td><code>new method()<\/code><\/td><td>\u65b0\u5bf9\u8c61<\/td><\/tr><tr><td>\u76f4\u63a5\u8c03\u7528<\/td><td><code>method()<\/code><\/td><td>\u5168\u5c40\u5bf9\u8c61<\/td><\/tr><tr><td>\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528<\/td><td><code>obj.method()<\/code><\/td><td>obj<\/td><\/tr><tr><td>call\u3001apply\u3001bind<\/td><td><code>method.call(ctx)<\/code><\/td><td>\u7b2c\u4e00\u4e2a\u53c2\u6570<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u524d\u5bfc\u77e5\u8bc6\uff1a&nbsp;<a href=\"https:\/\/www.baidu.com\/s?wd=js%E4%B8%AD%E7%9A%84new%20&amp;oq=js%25E5%25A0%2586%25E6%25A0%2588%25E6%2595%25B0%25E6%258D%25AE%25E7%25BB%2593%25E6%259E%2584\"><code>new<\/code>&nbsp;\u5173\u952e\u5b57\u7684\u6267\u884c\u8fc7\u7a0b<\/a>&nbsp;|&nbsp;<a href=\"https:\/\/www.cnblogs.com\/buildnewhomeland\/p\/12797537.html\">\u770b\u8fd9\u4e2a\u4e5f\u884c<\/a><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u521b\u5efa<code>\u7a7a\u5bf9\u8c61{}<\/code><\/li><li><code>\u7a7a\u5bf9\u8c61{}<\/code>&nbsp;\u7684&nbsp;<code>__proto__<\/code>&nbsp;-&gt;&nbsp;<code>\u6784\u9020\u51fd\u6570.prototype<\/code><\/li><li>\u8c03\u7528&nbsp;<code>\u6784\u9020\u51fd\u6570.call()<\/code>&nbsp;\u5c06&nbsp;<code>this<\/code>&nbsp;-&gt;<code>window<\/code>&nbsp;\u6539\u4e3a&nbsp;<code>this<\/code>&nbsp;-&gt;&nbsp;<code>\u7a7a\u5bf9\u8c61{}<\/code>\uff0c\u5e76\u8fdb\u884c\u5b9e\u53c2\u8d4b\u503c<\/li><li>\u6267\u884c<code>\u6784\u9020\u51fd\u6570<\/code>\u51fd\u6570\u4f53\u7684\u5185\u5bb9<\/li><li>\u8fd4\u56de<code>\u7a7a\u5bf9\u8c61{}<\/code>&nbsp;\uff08\u6216\u8005\u51fd\u6570\u4f53\u5185\u7684return\u8bed\u53e5\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * \u7b2c\u4e00\u79cd\uff1anew\u5173\u952e\u5b57\n *\/<\/em>\nfunction test(params) {\n    console.log(this);\n}\nnew test();     <em>\/\/  this -&gt; \u65b0\u5bf9\u8c61(new\u751f\u6210\u7684\u8fd9\u4e2a\u5b9e\u4f8b)<\/em>\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * \u7b2c\u4e8c\u79cd\uff1a\u5168\u5c40\u4e0b\u8c03\u7528\n *  *\/<\/em>\nfunction test(params) {\n    console.log(this);\n}\ntest();     <em>\/\/ this -&gt; window&#91;Browser] or global&#91;Node]<\/em>\n            <em>\/\/ \u8fd9\u79cd\u7b49\u540c\u4e8e window.test()<\/em>\nconsole.log(this);     <em>\/\/ this -&gt; window&#91;Browser] or global&#91;Node]<\/em>\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * \u7b2c\u4e09\u79cd\uff1a\u5bf9\u8c61\u8c03\u7528\n *  *\/<\/em>\nfunction test(params) {\n    console.log(this);\n}\nvar obj = { fn: test }\uff1b\n\nobj.test(); <em>\/\/ this -&gt; obj<\/em>\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * \u7b2c\u56db\u79cd\uff1a\u7279\u6b8a\u65b9\u6cd5\u8c03\u7528 call\/apply\/bind\n *\n * - \u76f8\u540c\uff1a\u4e09\u8005\u90fd\u53ef\u4ee5\u6539\u53d8this\u5230\u7b2c\u4e00\u4e2a\u53c2\u6570\n * - \u4e0d\u540c\uff1a\n *      1.\u6267\u884c\u65f6\u95f4\n *          bind\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u9700\u8981\u4e3b\u52a8\u52a0()\u8c03\u7528\n *          call\u3001apply\u4f1a\u81ea\u52a8\u6267\u884c\n *      2.\u63a5\u53d7\u53c2\u6570\n *          call\u3001bind\u7684\u5269\u4f59\u53c2\u6570\u4ee5','\u5206\u5272\n *          apply\u7684\u5269\u4f59\u53c2\u6570\u653e\u5165\u4e00\u4e2a\u6570\u7ec4\n *\n *  *\/<\/em>\nvar test = {\n    name: '\u5f20\u4e09',\n    age: 17,\n    toStr: function (from, to) {\n        console.log(this.name + ' \u5e74\u9f84\uff1a' + this.age + \"\uff0c\u6765\u81ea \" + from + \" \u53bb\u5f80 \" + to);\n    }\n}\nvar obj = {name: '\u674e\u56db', age: 20};\n\ntest.toStr.call(obj, '\u6210\u90fd', '\u4e0a\u6d77');   <em>\/\/ \u674e\u56db \u5e74\u9f84\uff1a20\uff0c\u6765\u81ea \u6210\u90fd \u53bb\u5f80 \u4e0a\u6d77<\/em>\ntest.toStr.apply(obj, &#91;'\u6210\u90fd', '\u4e0a\u6d77']);    <em>\/\/ \u674e\u56db \u5e74\u9f84\uff1a20\uff0c\u6765\u81ea \u6210\u90fd \u53bb\u5f80 \u4e0a\u6d77<\/em>\ntest.toStr.bind(obj, '\u6210\u90fd', '\u4e0a\u6d77')();     <em>\/\/ \u674e\u56db \u5e74\u9f84\uff1a20\uff0c\u6765\u81ea \u6210\u90fd \u53bb\u5f80 \u4e0a\u6d77<\/em>\ntest.toStr.bind(obj, &#91;'\u6210\u90fd', '\u4e0a\u6d77'])();   <em>\/\/ \u674e\u56db \u5e74\u9f84\uff1a20\uff0c\u6765\u81ea \u6210\u90fd,\u4e0a\u6d77 \u53bb\u5f80 undefined<\/em><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u5bfc\u77e5\u8bc6&nbsp;\u539f\u6587&nbsp;|&nbsp;\u5907\u4efd: Javascript\u4e2d\u7684&nbsp;this&#038;nbs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[23,26],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-js","tag-js","tag-this"],"_links":{"self":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=150"}],"version-history":[{"count":0,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts\/150\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}