{"id":3292,"date":"2024-11-10T08:08:53","date_gmt":"2024-11-10T00:08:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3292\/"},"modified":"2024-11-10T08:08:53","modified_gmt":"2024-11-10T00:08:53","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","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3292\/","title":{"rendered":"JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173102835372349.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<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>const arr = [1, 2, 3];\n\narr.map((num) =&gt; {\n  console.log(this);  \n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f\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>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 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b const arr = [1, 2, 3]; arr.map((num) =&gt; { console.log(this); }); \u767b\u5f55\u540e\u590d\u5236 \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 const myObject = {a: 1}; arr.map((num) =&gt; { console.log(this); [&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-3292","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3292","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=3292"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3292\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}