{"id":3328,"date":"2024-11-10T17:20:39","date_gmt":"2024-11-10T09:20:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3328\/"},"modified":"2024-11-10T17:20:39","modified_gmt":"2024-11-10T09:20:39","slug":"js%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8a%a8%e6%80%81%e7%bb%91%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3328\/","title":{"rendered":"js\u4e2d\u5982\u4f55\u52a8\u6001\u7ed1\u5b9a"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\uff0c\u53ef\u901a\u8fc7 function.prototype.bind() \u65b9\u6cd5\u5b9e\u73b0\u52a8\u6001\u7ed1\u5b9a\uff0c\u5c06\u76ee\u6807\u51fd\u6570\u4e0e\u7279\u5b9a this \u503c\u7ed1\u5b9a\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u66f4\u6539\u5bf9\u8c61\u884c\u4e3a\u3002\u4f18\u70b9\u5305\u62ec\u81ea\u5b9a\u4e49\u5bf9\u8c61\u884c\u4e3a\u3001\u63d0\u9ad8\u4ee3\u7801\u53ef\u91cd\u7528\u6027\u4ee5\u53ca\u521b\u5efa\u56de\u8c03\u51fd\u6570\u3002\u4f46\u5c40\u9650\u6027\u5728\u4e8e\u53ef\u80fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7406\u89e3\uff0c\u4ee5\u53ca\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110809462082101.jpg\" class=\"aligncenter\" title=\"js\u4e2d\u5982\u4f55\u52a8\u6001\u7ed1\u5b9a\u63d2\u56fe\" alt=\"js\u4e2d\u5982\u4f55\u52a8\u6001\u7ed1\u5b9a\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u5b9e\u73b0\u52a8\u6001\u7ed1\u5b9a<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><br \/>\u52a8\u6001\u7ed1\u5b9a\u662f\u4e00\u79cd\u5728\u8fd0\u884c\u65f6\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u7ed1\u5b9a\u51fd\u6570\u6216\u65b9\u6cd5\u5230\u5bf9\u8c61\u7684\u673a\u5236\u3002\u5728 JavaScript \u4e2d\uff0c\u5b83\u5141\u8bb8\u60a8\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u6539\u53d8\u5bf9\u8c61\u7684\u884c\u4e3a\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u52a8\u6001\u7ed1\u5b9a<\/strong><br \/>\u5728 JavaScript \u4e2d\uff0c\u52a8\u6001\u7ed1\u5b9a\u53ef\u4ee5\u4f7f\u7528 Function.prototype.bind() \u65b9\u6cd5\u5b9e\u73b0\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5b83\u5c06\u76ee\u6807\u51fd\u6570\u4e0e\u7279\u5b9a\u7684 this \u503c\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>bind(thisArg, ...arg1, ...arg2, ...)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>thisArg\uff1a\u5c06\u7ed1\u5b9a\u5230\u65b0\u51fd\u6570\u7684 this \u503c\u3002<\/li>\n<li>&#8230;arg1, &#8230;arg2, &#8230;\uff1a\u8981\u9884\u5148\u586b\u5145\u5230\u65b0\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u4e2d\u7684\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre>const obj = {\n  name: 'John',\n  sayHello: function() {\n    console.log(`Hello, my name is ${this.name}!`);\n  }\n};\n\nconst boundHello = obj.sayHello.bind(obj);\n\nboundHello(); \/\/ \u8f93\u51fa\uff1aHello, my name is John!<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cboundHello \u662f\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u5b83\u5c06 this \u503c\u7ed1\u5b9a\u5230 obj\u3002\u8fd9\u610f\u5473\u7740\u5f53\u8c03\u7528 boundHello \u65f6\uff0cthis.name \u5c06\u5f15\u7528 obj.name\uff0c\u5373\u4f7f boundHello \u5728\u53e6\u4e00\u4e2a\u4e0a\u4e0b\u6587\u4e2d\u8c03\u7528\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<ul>\n<li>\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u7684\u884c\u4e3a\u3002<\/li>\n<li>\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u56e0\u4e3a\u60a8\u53ef\u4ee5\u5c06\u51fd\u6570\u7ed1\u5b9a\u5230\u4e0d\u540c\u7684\u5bf9\u8c61\u4e0a\u3002<\/li>\n<li>\u6709\u52a9\u4e8e\u521b\u5efa\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u5728\u7279\u5b9a\u7684 this \u503c\u4e0a\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<p><strong>\u5c40\u9650\u6027<\/strong><\/p>\n<ul>\n<li>\u53ef\u80fd\u4f1a\u4f7f\u4ee3\u7801\u66f4\u96be\u9605\u8bfb\u548c\u7406\u89e3\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u4e0d\u719f\u6089\u52a8\u6001\u7ed1\u5b9a\u7684\u5f00\u53d1\u4eba\u5458\u3002<\/li>\n<li>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u4e3a\u7ed1\u5b9a\u7684\u51fd\u6570\u5c06\u59cb\u7ec8\u5f15\u7528\u539f\u59cb\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u4e2d\u5982\u4f55\u52a8\u6001\u7ed1\u5b9a\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\uff0c\u53ef\u901a\u8fc7 function.prototype.bind() \u65b9\u6cd5\u5b9e\u73b0\u52a8\u6001\u7ed1\u5b9a\uff0c\u5c06\u76ee\u6807\u51fd\u6570\u4e0e\u7279\u5b9a this \u503c\u7ed1\u5b9a\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u66f4\u6539\u5bf9\u8c61\u884c\u4e3a\u3002\u4f18\u70b9\u5305\u62ec\u81ea\u5b9a\u4e49\u5bf9\u8c61\u884c\u4e3a\u3001\u63d0\u9ad8\u4ee3\u7801\u53ef\u91cd\u7528\u6027\u4ee5\u53ca\u521b\u5efa\u56de\u8c03\u51fd\u6570\u3002\u4f46\u5c40\u9650\u6027\u5728\u4e8e\u53ef\u80fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7406\u89e3\uff0c\u4ee5\u53ca\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u5b9e\u73b0\u52a8\u6001\u7ed1\u5b9a \u7b80\u4ecb\u52a8\u6001\u7ed1\u5b9a\u662f\u4e00\u79cd\u5728\u8fd0\u884c\u65f6\u800c\u4e0d\u662f\u7f16\u8bd1\u65f6\u7ed1\u5b9a\u51fd\u6570\u6216\u65b9\u6cd5\u5230\u5bf9\u8c61\u7684\u673a\u5236\u3002\u5728 JavaScript \u4e2d\uff0c\u5b83\u5141\u8bb8\u60a8\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u6539\u53d8\u5bf9\u8c61\u7684\u884c\u4e3a\u3002 \u5b9e\u73b0\u52a8\u6001\u7ed1\u5b9a\u5728 JavaScript \u4e2d\uff0c\u52a8\u6001\u7ed1\u5b9a\u53ef\u4ee5\u4f7f\u7528 Function.prototype.bind() \u65b9\u6cd5\u5b9e\u73b0\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5b83\u5c06\u76ee\u6807\u51fd\u6570\u4e0e\u7279\u5b9a\u7684 this \u503c\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u8bed\u6cd5 bind(thisArg, &#8230;arg1, &#8230;arg2, &#8230;) \u767b\u5f55\u540e\u590d\u5236 thisArg\uff1a\u5c06\u7ed1\u5b9a\u5230\u65b0\u51fd\u6570\u7684 this \u503c\u3002 &#8230;arg1, &#8230;arg2, &#8230;\uff1a\u8981\u9884\u5148\u586b\u5145\u5230\u65b0\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u4e2d\u7684\u503c\u3002 \u793a\u4f8b const obj = { name: &#8216;John&#8217;, sayHello: function() { console.log(`Hello, my name is ${this.name}!`); } }; const boundHello = obj.sayHello.bind(obj); boundHello(); \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3328","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3328"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3328\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}