{"id":1284,"date":"2022-06-19T08:41:40","date_gmt":"2022-06-19T00:41:40","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1284"},"modified":"2022-06-19T15:12:33","modified_gmt":"2022-06-19T07:12:33","slug":"40-zhen-zheng-li-jie-bindcall-he-apply","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1284","title":{"rendered":"40 &#8211; \u771f\u6b63\u7406\u89e3 Bind\u3001Call \u548c Apply"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16555993055272.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/truly-understand-bind-call-apply-21da\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/truly-understand-bind-call-apply-21da<\/a><\/p>\n<h2><a id=\"bind\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Bind<\/h2>\n<p>JavaScript \u4e2d\u7684 <code>this<\/code> \u626e\u6f14\u8005\u91cd\u8981\u7684\u89d2\u8272\uff0c\u5728 JavaScript \u4e2d\u5b83\u662f\u4f9d\u636e\u51fd\u6570\u662f\u600e\u4e48\u88ab\u8c03\u7528\u7684\u800c\u4e0d\u662f\u5b83\u5728\u54ea\u58f0\u660e\u7684\uff08\u7bad\u5934\u51fd\u6570\u5219\u8868\u73b0\u4e3a\u76f8\u53cd\uff09\u3002<\/p>\n<p>\u6211\u4eec\u4e3e\u4e00\u4e2a\u4f8b\u5b50\u6765\u793a\u8303\u4e00\u4e0b <code>this<\/code> \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const sayGreeting = {\n    name: &quot;Parwinder&quot;,\n    hello: function() {\n        return `Hello, ${this.name}`;\n    }\n}\n\nconsole.log(sayGreeting.hello()); \/\/ Hello, Parwinder\n<\/code><\/pre>\n<p><code>hello<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6 <code>sayGreeting<\/code> \u4e0a\u7684 <code>name<\/code> \u5c5e\u6027\uff0c\u5f53\u6211\u7528 <code>sayGreeting<\/code> \u8c03\u7528\u5b83\u65f6\uff0c\u5b83\u662f\u8fd0\u884c\u5728 <code>sayGreeting<\/code> \u5bf9\u8c61\u4e0a\u4e0b\u6587\u4e2d\u7684\u3002<\/p>\n<p>\u76f8\u53cd\u5982\u679c\u6211\u8fd9\u6837\u505a\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const sayGreeting = {\n    name: &quot;Parwinder&quot;,\n    hello: function() {\n        return `Hello, ${this.name}`;\n    }\n}\n\nconst hello = sayGreeting.hello;\n\nconsole.log(hello === sayGreeting.hello); \/\/ true\nconsole.log(hello()); \/\/ Hello, undefined\n<\/code><\/pre>\n<p>\u5c3d\u7ba1 <code>hello<\/code> \u53d8\u91cf\u4e0e <code>sayGreeting<\/code> \u5bf9\u8c61\u4e0a\u7684 <code>hello<\/code> \u65b9\u6cd5\u76f8\u7b49\uff0c\u4f46\u5b83\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u5e76\u4e0d\u662f\u5728 <code>sayGreeting<\/code> \u4e2d\uff0c\u800c\u662f\u5728 <code>window<\/code> \u6216\u5168\u5c40\u72b6\u6001\u4e2d\u3002<\/p>\n<p><code>bind<\/code> \u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u7ed1\u5b9a\u4e0a\u4e0b\u6587\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5\u4e14\u4e0a\u4e0b\u6587\u7ed1\u5b9a\u4e3a\u6211\u4eec\u4f20\u9012\u7ed9 <code>bind<\/code> \u51fd\u6570\u7684\u5185\u5bb9\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const sayGreeting = {\n    name: &quot;Parwinder&quot;,\n    hello: function() {\n        return `Hello, ${this.name}`;\n    }\n}\n\nconst hello = sayGreeting.hello.bind(sayGreeting);\nconsole.log(hello()); \/\/ Hello, Parwinder\n<\/code><\/pre>\n<h3><a id=\"%E5%AE%9E%E9%99%85%E4%B8%9A%E5%8A%A1%E4%B8%AD%E5%93%AA%E9%87%8C%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8bind\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5b9e\u9645\u4e1a\u52a1\u4e2d\u54ea\u91cc\u9700\u8981\u4f7f\u7528 bind?<\/h3>\n<p>\u4e0a\u9762\u7684\u6240\u6709\u4f8b\u5b50\uff0c\u6570\u636e\u7684\u83b7\u53d6\u548c\u65b9\u6cd5\u7684\u8c03\u7528\u90fd\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e0a bind \u7684\u4f5c\u7528\u5e76\u4e0d\u660e\u663e\u3002\u53ef\u6709\u65f6\u5019\u5f53\u4f60\u9700\u8981\u5411\u4e00\u4e2a\u5bf9\u8c61\u501f\u4e00\u4e2a\u65b9\u6cd5\u4f46\u8fd0\u884c\u4e0a\u4e0b\u6587\u9700\u8981\u5728\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u65f6\uff0c\u4f60\u5c31\u9700\u8981\u4f7f\u7528 <code>bind<\/code>\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const sayGreeting = {\n    name: &quot;Parwinder&quot;,\n    hello: function () {\n        return `Hello, ${this.name}`;\n    }\n}\n\nconst nameObject = {\n    name: &quot;Lauren&quot;\n}\n\nconst hello = sayGreeting.hello.bind(nameObject);\n\nconsole.log(hello()); \/\/ Hello, Lauren\n<\/code><\/pre>\n<p><code>sayGreeting<\/code> \u5bf9\u8c61\u4e0a\u6709 <code>hello<\/code> \u65b9\u6cd5\uff0c\u6240\u4ee5\u5728 <code>nameObject<\/code> \u5bf9\u8c61\u4e0a\u5c31\u6ca1\u6709\u5fc5\u8981\u518d\u5199\u4e00\u4e2a\u76f8\u540c\u7684\u65b9\u6cd5\u3002\u6211\u53ef\u4ee5\u5411 <code>sayGreeting<\/code> \u5bf9\u8c61\u501f\u7528\u5b83\u7136\u540e\u8fd0\u884c\u5728 <code>nameObject<\/code> \u4e0a\u4e0b\u6587\u4e2d\u3002<\/p>\n<h2><a id=\"call\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Call<\/h2>\n<p><code>call<\/code> \u548c <code>apply<\/code> \u4e0e <code>bind<\/code> \u662f\u6709\u533a\u522b\u7684\uff0c<code>bind<\/code> \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5\u800c <code>call<\/code> \u548c <code>apply<\/code> \u5219\u7acb\u5373\u8c03\u7528\u6267\u884c\u65b9\u6cd5\u3002<code>call<\/code> \u628a <code>this<\/code> \u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u5176\u4ed6\u53c2\u6570\u9700\u8981\u4e00\u4e2a\u4e2a\u7684\u4f20\u9012\u3002\u5b83\u4eec\u90fd\u4f1a\u4f20\u9012\u5230\u6211\u4eec\u8c03\u7528\u7684\u51fd\u6570\u4e2d\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const sayGreeting = {\n    name: &quot;Parwinder&quot;,\n    hello: function () {\n        return `Hello, ${this.name}`;\n    }\n}\n\nconsole.log(sayGreeting.hello.call(sayGreeting)); \/\/ Hello, Parwinder\n<\/code><\/pre>\n<p>\u5e26\u6709\u5176\u5b83\u53c2\u6570\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const sayGreeting = {\n    name: &quot;Parwinder&quot;,\n    hello: function (trait, color) {\n        return `Hello, ${this.name}. I see you ${trait} ${color}. It is my favorite too!`;\n    }\n}\n\nconsole.log(sayGreeting.hello.call(sayGreeting, &quot;like&quot;, &quot;red&quot;));\n\/\/ Hello, Parwinder. I see you like red. It is my favorite too!\n<\/code><\/pre>\n<h2><a id=\"apply\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Apply<\/h2>\n<p>\u5c3d\u7ba1 <code>apply<\/code> \u548c <code>call<\/code> \u7c7b\u4f3c\u90fd\u662f\u76f4\u63a5\u6267\u884c\u51fd\u6570\uff0c\u4f46\u5b83\u63a5\u53d7\u7684\u662f\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u800c\u4e0d\u662f\u9017\u53f7\u5206\u9694\u7684\u503c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const sayGreeting = {\n    name: &quot;Parwinder&quot;,\n    hello: function () {\n        return `Hello, ${this.name}`;\n    }\n}\n\nconsole.log(sayGreeting.hello.apply(sayGreeting)); \/\/ Hello, Parwinder\n<\/code><\/pre>\n<p>\u5f53\u6ca1\u6709\u5176\u4f59\u53c2\u6570\u65f6 <code>apply<\/code> \u548c <code>call<\/code> \u6ca1\u6709\u533a\u522b\uff0c\u4f46\u662f\u5f53\u6211\u4eec\u6709\u5176\u4f59\u53c2\u6570\u65f6\uff0c\u4f7f\u7528\u65b9\u6cd5\u5c31\u6709\u70b9\u533a\u522b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const sayGreeting = {\n    name: &quot;Parwinder&quot;,\n    hello: function (trait, color) {\n        return `Hello, ${this.name}. I see you ${trait} ${color}. It is my favorite too!`;\n    }\n}\n\nconsole.log(sayGreeting.hello.apply(sayGreeting, [&quot;like&quot;, &quot;red&quot;]));\n\/\/ Hello, Parwinder. I see you like red. It is my favorite too!\n<\/code><\/pre>\n<p><code>apply<\/code> \u4f7f\u5f97\u4f20\u9012\u591a\u4e2a\u53c2\u6570\u53d8\u5f97\u66f4\u5bb9\u6613\u4e9b\uff0c\u4f46\u662f\u73b0\u5728 ES6 \u4e2d\u4f7f\u7528\u6269\u5c55\u8fd0\u7b97\u7b26\u4f20\u9012\u591a\u4e2a\u53c2\u6570\u4e5f\u5f88\u5bb9\u6613\u4e86\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/\u524d\u7aef\u9ed1\u677f\u62a5-8.jpg\" alt=\"\u524d\u7aef\u9ed1\u677f\u62a5\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/truly-understa&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[155,153,154,6],"class_list":["post-1284","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-apply","tag-bind","tag-call","tag-javascript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1284"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1284\/revisions"}],"predecessor-version":[{"id":1285,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1284\/revisions\/1285"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}