{"id":53300,"date":"2024-12-03T17:22:01","date_gmt":"2024-12-03T09:22:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53300\/"},"modified":"2024-12-03T17:22:01","modified_gmt":"2024-12-03T09:22:01","slug":"javascript-%e4%b8%ad-array-map-%e5%92%8c-array-filter-%e7%9a%84-thisvalue-%e5%8f%82%e6%95%b0%e6%9c%89%e4%bb%80%e4%b9%88%e7%94%a8%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53300\/","title":{"rendered":"JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f <\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aJavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f \u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/1732839896674909d8b275c.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u4e2d\u7684 array.map() \u548c array.filter() \u65b9\u6cd5\u4e2d\u7684 thisvalue \u53c2\u6570<\/strong><\/p>\n<p>javascript \u5185\u7f6e\u7684 array.map() \u548c array.filter() \u65b9\u6cd5\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u5176\u7b7e\u540d\u4e2d\u6709\u4e00\u4e2a\u9c9c\u4e3a\u4eba\u77e5\u4f46\u975e\u5e38\u6709\u7528\u7684\u53c2\u6570\uff1athisvalue\u3002<\/p>\n<p><strong>thisvalue \u7684\u7528\u6cd5<\/strong><\/p>\n<p>thisvalue \u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u5728\u56de\u8c03\u51fd\u6570\u4e2d this \u5173\u952e\u5b57\u7684\u503c\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cthis \u5173\u952e\u5b57\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff08\u5728\u6d4f\u89c8\u5668\u4e2d\u4e3a window\uff09\u3002\u7136\u800c\uff0c\u901a\u8fc7\u6307\u5b9a thisvalue\uff0c\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a this \u5173\u952e\u5b57\u7684\u503c\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const arr = [1, 2, 3];\n\narr.map((num) =&gt; {\n  console.log(this);  \n});<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56de\u8c03\u51fd\u6570\u4e2d this \u7684\u9ed8\u8ba4\u503c\u662f window\uff0c\u56e0\u4e3a thisvalue \u6ca1\u6709\u88ab\u6307\u5b9a\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2a\u5bf9\u8c61\u6765\u6307\u5b9a thisvalue\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>const myObject = {a: 1};\n\narr.map((num) =&gt; {\n  console.log(this);  \n}, myObject);<\/pre>\n<p>\u73b0\u5728\uff0c\u5f53\u56de\u8c03\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0cthis \u5c06\u6307\u5411 myobject\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4ece\u56de\u8c03\u51fd\u6570\u4e2d\u8bbf\u95ee myobject \u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 this.a \u8bbf\u95ee myobject \u7684 a \u5c5e\u6027\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f \u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u66f4\u6539\u7535\u8111\u56fe\u6807\u5927\u5c0f\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aJavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f \u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 javascript \u4e2d\u7684 array.map() \u548c array.filter() \u65b9\u6cd5\u4e2d\u7684 thisvalue \u53c2\u6570 javascript \u5185\u7f6e\u7684 array.map() \u548c array.filter() \u65b9\u6cd5\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u5176\u7b7e\u540d\u4e2d\u6709\u4e00\u4e2a\u9c9c\u4e3a\u4eba\u77e5\u4f46\u975e\u5e38\u6709\u7528\u7684\u53c2\u6570\uff1athisvalue\u3002 thisvalue \u7684\u7528\u6cd5 thisvalue \u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u5728\u56de\u8c03\u51fd\u6570\u4e2d this \u5173\u952e\u5b57\u7684\u503c\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cthis \u5173\u952e\u5b57\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff08\u5728\u6d4f\u89c8\u5668\u4e2d\u4e3a window\uff09\u3002\u7136\u800c\uff0c\u901a\u8fc7\u6307\u5b9a thisvalue\uff0c\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a this \u5173\u952e\u5b57\u7684\u503c\u3002 \u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a const arr = [1, 2, 3]; arr.map((num) =&gt; { console.log(this); }); \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56de\u8c03\u51fd\u6570\u4e2d [&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-53300","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53300","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=53300"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53300\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}