{"id":3314,"date":"2024-11-10T12:55:44","date_gmt":"2024-11-10T04:55:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3314\/"},"modified":"2024-11-10T12:55:44","modified_gmt":"2024-11-10T04:55:44","slug":"javascript-%e4%b8%ad-array-map-%e5%92%8c-array-filter-%e7%9a%84-thisvalue-%e5%8f%82%e6%95%b0%e6%98%af%e5%81%9a%e4%bb%80%e4%b9%88%e7%94%a8%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3314\/","title":{"rendered":"JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173102958388320.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u4e2d array.map() \u548c array.filter() \u7684 thisvalue \u53c2\u6570<\/strong><\/p>\n<p>array.map() \u548c array.filter() \u662f javascript \u4e2d\u7ecf\u5e38\u7528\u4e8e\u6570\u7ec4\u5904\u7406\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002<\/p>\n<p><strong>thisvalue \u53c2\u6570<\/strong><\/p>\n<p>\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u9009\u7684 thisvalue \u53c2\u6570\u3002\u6b64\u53c2\u6570\u6307\u5b9a\u5728\u56de\u8c03\u51fd\u6570\u4e2d this \u5173\u952e\u5b57\u7684\u503c\u3002\u901a\u5e38\uff0cthis \u5173\u952e\u5b57\u5f15\u7528\u8c03\u7528\u56de\u8c03\u51fd\u6570\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u901a\u8fc7\u4f7f\u7528 thisvalue \u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u5f0f\u5730\u8bbe\u7f6e this \u7684\u503c\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>thisvalue \u53c2\u6570\u63a5\u53d7\u4e00\u4e2a\u503c\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\uff0c\u4f8b\u5982\u5bf9\u8c61\u3001\u6570\u7ec4\u6216\u539f\u59cb\u503c\u3002\u5728\u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5173\u952e\u5b57\u5c06\u5f15\u7528\u6b64\u503c\u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 thisvalue \u53c2\u6570\uff1a<\/p>\n<pre>const obj = { a: 1 };\n[1, 2, 3].map(function() {\n  console.log(this); \/\/ \u5728\u56de\u8c03\u51fd\u6570\u91cc\u4f7f\u7528 this\uff0c\u8fd9\u4e2a this \u5c31\u6307\u5411\u90a3\u4e2a {a: 1}\n}, obj);\n\/\/ \u8f93\u51fa\uff1a{ a: 1 }, { a: 1 }, { a: 1 }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0cthisvalue \u53c2\u6570\u8bbe\u7f6e\u4e3a {a: 1} \u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5728\u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5173\u952e\u5b57\u5f15\u7528 {a: 1} \u5bf9\u8c61\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d Array.map() \u548c Array.filter() \u7684 thisValue \u53c2\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\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 array.map() \u548c array.filter() \u7684 thisvalue \u53c2\u6570 array.map() \u548c array.filter() \u662f javascript \u4e2d\u7ecf\u5e38\u7528\u4e8e\u6570\u7ec4\u5904\u7406\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002 thisvalue \u53c2\u6570 \u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u9009\u7684 thisvalue \u53c2\u6570\u3002\u6b64\u53c2\u6570\u6307\u5b9a\u5728\u56de\u8c03\u51fd\u6570\u4e2d this \u5173\u952e\u5b57\u7684\u503c\u3002\u901a\u5e38\uff0cthis \u5173\u952e\u5b57\u5f15\u7528\u8c03\u7528\u56de\u8c03\u51fd\u6570\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u901a\u8fc7\u4f7f\u7528 thisvalue \u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u5f0f\u5730\u8bbe\u7f6e this \u7684\u503c\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u7528\u6cd5 thisvalue \u53c2\u6570\u63a5\u53d7\u4e00\u4e2a\u503c\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\uff0c\u4f8b\u5982\u5bf9\u8c61\u3001\u6570\u7ec4\u6216\u539f\u59cb\u503c\u3002\u5728\u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5173\u952e\u5b57\u5c06\u5f15\u7528\u6b64\u503c\u3002 \u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 thisvalue \u53c2\u6570\uff1a const obj = { a: 1 }; [1, 2, 3].map(function() { console.log(this); \/\/ \u5728\u56de\u8c03\u51fd\u6570\u91cc\u4f7f\u7528 this\uff0c\u8fd9\u4e2a this \u5c31\u6307\u5411\u90a3\u4e2a {a: 1} }, obj); [&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-3314","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3314","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=3314"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3314\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}