{"id":11387,"date":"2024-10-24T22:57:18","date_gmt":"2024-10-24T14:57:18","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11387"},"modified":"2024-10-24T22:57:18","modified_gmt":"2024-10-24T14:57:18","slug":"js-object-is-array-at-substring-slice","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2024\/10\/js-object-is-array-at-substring-slice\/","title":{"rendered":"Object.is\/===\u3001\u6570\u7ec4at\/\u76f4\u63a5\u7d22\u5f15\u3001substring\/slice\u7684\u533a\u522b"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11387\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11387<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u72ec\u7acb\u57df\u540d\u4e2a\u4eba\u7f51\u7ad9\u65e0\u9700\u6388\u6743\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202410-js-diff.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202410\/js-bird-cover.jpg\" width=\"413\" height=\"262\" alt=\"\u559c\u9e4a\u9e1f\u7c7b\u5360\u4f4d\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001Object.is\u548c===\u7684\u533a\u522b<\/h3>\n<p><code>Object.is()<\/code>\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u4e25\u683c\u76f8\u7b49\uff0c\u5728\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u548c\u4e09\u7b49\u4e8e\u53f7===\u7684\u4f5c\u7528\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u9664\u4e86\u4e0b\u9762\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<h4>1. NaN\u7684\u6bd4\u8f83<\/h4>\n<p>\u5982\u679c\u4f7f\u7528\u7b49\u4e8e\u597d\u6bd4\u8f83\uff0c\u5219NaN\u548cNaN\u7684\u8fd4\u56de\u7ed3\u679c\u662f<code>false<\/code>\uff1a<\/p>\n<pre>NaN === NaN\r\n\/\/ false<\/pre>\n<p>\u4f46\u4f7f\u7528<code>Object.is()<\/code>\u6bd4\u8f83\u5219\u8fd4\u56de\u7684\u662f<code>true<\/code>\uff1a<\/p>\n<pre>Object.is(NaN, NaN)\r\n\/\/ true<\/pre>\n<h4>2. +0\u548c-0\u7684\u6bd4\u8f83<\/h4>\n<p>\u4e00\u53e5\u8bdd\uff0c=== \u662f\u8ba4\u4e3a\u76f8\u7b49\u7684\uff0c\u800c<code>Object.is()<\/code>\u8ba4\u4e3a\u662f\u4e0d\u76f8\u7b49\u7684\uff0c\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>+0 === -0\r\n\/\/ true\r\n\r\nObject.is(+0, -0)\r\n\/\/ false<\/pre>\n<h3>\u4e8c\u3001\u6570\u7ec4at\u65b9\u6cd5\u548c\u76f4\u63a5\u7d22\u5f15\u7684\u533a\u522b<\/h3>\n<p>\u5df2\u77e5\u4e00\u4e2a\u6570\u7ec4 <code>arr = [1,2,3]<\/code>\uff0c\u5219\u4e0b\u9762\u4e24\u79cd\u5199\u6cd5\u5747\u7b49\u8fd4\u56de\u6570\u7ec4\u7684\u7b2c\u4e8c\u9879\u5185\u5bb9\uff1a<\/p>\n<pre>const arr = [1,2,3];\r\nconsole.log(arr.at(1), arr[1]);\r\n\/\/ \u8fd4\u56de\u7ed3\u679c\uff1a2 2<\/pre>\n<p>\u90a3\u95ee\u9898\u6765\u4e86\uff0c\u6570\u7ec4\u4f7f\u7528at()\u65b9\u6cd5\u83b7\u53d6\u9009\u9879\uff0c\u548c\u76f4\u63a5\u4f7f\u7528\u7d22\u5f15\u83b7\u53d6\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n<p>\u533a\u522b\u5c31\u5728\u4e8e\u53c2\u6570\u6216\u7d22\u5f15\u503c\u4e3a\u8d1f\u6570\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50\uff1a<\/p>\n<pre>const arr = [1,2,3];\r\nconsole.log(arr.at(-1), arr[-1]);\r\n\/\/ \u8fd4\u56de\u7ed3\u679c\uff1a3 undefined<\/pre>\n<p>\u662f\u8fd9\u6837\u7684\uff0c\u5f53<code>Array.prototype.at()<\/code>\u65b9\u6cd5\u7684\u53c2\u6570\u662f\u8d1f\u6570\u7684\u65f6\u5019\uff0c\u5176\u53c2\u6570\u503c\u7b49\u4e8e\u8be5\u8d1f\u6570\u548c\u6574\u4e2a\u6570\u7ec4\u957f\u5ea6\u7684\u548c\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>arr.at(-1)<\/code>\u5176\u5b9e\u7b49\u4e8e <code>arr.at(-1 + 3) =&gt; arr.at(2) =&gt; 3<\/code><\/p>\n<p>\u8fd9\u5c31\u662f\u6700\u540e\u7ed3\u679c3\u7684\u7531\u6765\u3002<\/p>\n<p>\u5982\u679c\u8d1f\u6570\u7684\u7edd\u5bf9\u503c\u6bd4\u6570\u7ec4\u7684\u957f\u5ea6\u8fd8\u8981\u5927\uff0c\u6216\u8005\u53c2\u6570\u662f\u6b63\u503c\uff0c\u4f46\u662f\u8d85\u51fa\u6570\u7ec4\u8303\u56f4\uff0c\u5219\u8fd4\u56de<code>undefined<\/code>\uff0c\u56e0\u6b64\uff0c\u5c31\u4f1a\u6709\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\u8fd0\u884c\u6548\u679c\u3002<\/p>\n<pre>const arr = [1,2,3];\r\nconsole.log(arr.at(-4), arr[-4]);\r\n\/\/ \u8fd4\u56de\u7ed3\u679c\uff1aundefined undefined\r\n\r\nconsole.log(arr.at(3), arr[3]);\r\n\/\/ \u8fd4\u56de\u7ed3\u679c\uff1aundefined undefined<\/pre>\n<h3>\u4e09\u3001substring\/slice\u7684\u533a\u522b<\/h3>\n<p>\u5b57\u7b26\u4e32\u7684<code>substring()<\/code>\u548c<code>slice()<\/code>\u65b9\u6cd5\u5728\u7edd\u5927\u6570\u65f6\u5019\u662f\u901a\u7528\u7684\uff0c\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u590d\u5236\u3002<\/p>\n<p>\u533a\u522b\u5728\u4e8e\u4e0b\u9762\u8fd9\u4e24\u79cd\u573a\u666f\uff1a<\/p>\n<ol>\n<li>substring()\u7684\u53c2\u6570\u4e3a\u8d1f\u65f6\uff0c\u89c6\u4e3a0\uff0cslice()\u7684\u5141\u8bb8\u8d1f\u6570\u53c2\u6570\uff0c\u4f8b\u5982\uff1a\n<pre>'HTML\u5e76\u4e0d\u7b80\u5355'.substring(-2)\r\n\/\/ \u8fd4\u56de 'HTML\u5e76\u4e0d\u7b80\u5355'\r\n\r\n'HTML\u5e76\u4e0d\u7b80\u5355'.slice(-2)\r\n\/\/ \u8fd4\u56de '\u7b80\u5355'<\/pre>\n<\/li>\n<li>substring()\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5927\u4e8e\u7b2c\u4e8c\u4e2a\u65f6\uff0c\u4f1a\u81ea\u52a8\u4ea4\u6362\uff0c\u800cslice()\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5927\u4e8e\u7b2c\u4e8c\u4e2a\u65f6\uff0c\u8f93\u51fa\u7a7a\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a\n<pre>'HTML\u5e76\u4e0d\u7b80\u5355'.substring(4, 2)\r\n\/\/ \u8fd4\u56de 'ML'\r\n\r\n'HTML\u5e76\u4e0d\u7b80\u5355'.slice(4, 2)\r\n\/\/ \u8fd4\u56de ''<\/pre>\n<\/li>\n<\/ol>\n<p>\u5982\u679c\u5927\u5bb6\u65e0\u6cd5\u4fdd\u8bc1\u53c2\u6570\u7684\u524d\u540e\u5927\u5c0f\u987a\u5e8f\uff0c\u5219\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>substring()<\/code>\u65b9\u6cd5\uff0c\u5176\u4f59\u4f7f\u7528\uff0c\u5747\u5efa\u8bae\u4f7f\u7528<code>slice()<\/code>\u65b9\u6cd5\uff0c\u539f\u56e0\u65e0\u4ed6\uff0c\u540d\u79f0\u7b80\u77ed\u66f4\u597d\u8bb0\u5fc6\u3002<\/p>\n<h3>\u56db\u3001\u7ed3\u675f\u8bed<\/h3>\n<p>JS\u4e2d\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u4f5c\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u548c\u5199\u6cd5\uff0c\u8fd9\u5c31\u5c31\u4e0d\u4e00\u4e00\u4e3e\u4f8b\u4e86\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u662f\u626f\u6de1\u788e\u788e\u5ff5\u65f6\u95f4\u3002<\/p>\n<p>\u4e0a\u5468\u4e94\u53bb\u4f53\u68c0\u4e86\uff0c\u548c\u4e4b\u524d\u51e0\u5e74\u4e00\u6837\uff0c\u8fd9\u4e2a\u5f69\u7167\u554a\uff0c\u6162\u5f97\u8981\u6b7b\u3002\u53bb\u5e74\u5c31\u8bf4\u4e0d\u6765\u8fd9\u5bb6\uff0c\u7ed3\u679c\u4eca\u5e74\u8fd8\u6765\uff0c\u5509\uff0c\u516c\u53f8\u4f53\u68c0\u90a3\u51e0\u4e2a\u5355\u4f4d\uff0c\u5bb6\u9644\u8fd1\u5c31\u8fd9\u4e00\u4e2a\u3002<\/p>\n<p>\u597d\u5728\u8fd9\u4e00\u6b21\u8bf7\u5047\u4e86\uff0c\u65f6\u95f4\u4e0d\u6025\uff0c\u4e14\u65e9\u6709\u9884\u671f\uff0c\u6ca1\u6709\u4e4b\u524d\u51e0\u5e74\u90a3\u4e48\u4ee4\u4eba\u6293\u72c2\u3002<\/p>\n<p>\u5c0f\u670b\u53cb\u5468\u4e94\u79cb\u6e38\uff0c\u4ece\u8d77\u5e8a\u9192\u6765\u5230\u56de\u5bb6\uff0c\u5634\u5df4\u4e50\u5475\u5f97\u5c31\u6ca1\u5408\u4e0a\u8fc7\uff0c\u53bb\u6d77\u660c\u6d77\u6d0b\u9986\u770b\u4e86\u5fc3\u5fc3\u5ff5\u5ff5\u7684\u2026\u2026\u2026\u2026\u2026\u2026\u5965\u7279\u66fc\uff01\u8fd9\u73a9\u610f\uff0c\u5567\u5567\u5567\uff0c\u771f\u662f\u70e7\u91d1\u5e93\u3002\u95e8\u7968150\u5c31\u4e0d\u8bf4\u4e86\uff0c\u5c31\u90a3\u8367\u5149\u68d2\uff0c\u5c31\u662f\u6f14\u5531\u4f1a\u7684\u90a3\u79cd\u8367\u5149\u68d2\uff0c\u5916\u9762\u51e0\u5757\u94b1\u4e00\u4e2a\u7684\uff0c\u4f60\u731c\u8fd9\u91cc\u591a\u5c11\u94b1\u4e00\u6839\u3002\u8d85\u4e4e\u4f60\u7684\u60f3\u8c61\uff0c128\u5757\u94b1\u4e00\u4e2a\uff0c128\u5440\uff01\u8bf4\u662f\u7ed9\u5965\u7279\u66fc\u4ec0\u4e48\u5149\u7684\u529b\u91cf\u590d\u6d3b\u7684\uff0c\u8fd8\u6709\u62ab\u98ce\u5565\u7684\uff0c\u90fd\u662f\u5929\u4ef7\uff0c\u54e6\uff0c\u8fd8\u6709\uff0c300\u591a\u5757\u94b1\u8fd8\u53ef\u4ee5\u548c\u5965\u7279\u66fc\u5408\u5f715\u5f20\u7167\u7247\u3002\u5173\u952e\uff0c\u4e70\u7684\u4eba\u5de8\u591a\uff0c\u6211\u53ea\u80fd\u8bf4\uff0c\u4e0a\u6d77\u6709\u94b1\u4eba\u771f\u591a\u3002<\/p>\n<p>\u5468\u672b\u4e24\u5929\u53bb\u4e61\u4e0b\u6253\u5361\u9c7c\u5858\u7684\u65f6\u5019\u7684\u65f6\u5019\uff0c\u91c7\u8d2d\u4e86\u5f88\u591a\u8461\u8404\uff0c\u90fd\u5f88\u4fbf\u5b9c\uff0c5\u5757\u4e00\u65a4\uff0c\u6700\u8d35\u7684\u4e5f\u5c318\u5757\u94b1\uff0c\u4e70\u4e86\u597d\u591a\uff0c\u591a\u5230\u90fd\u5e26\u516c\u53f8\u6765\u5403\u4e86\u3002<\/p>\n<p>\u542c\u79cd\u8461\u8404\u7684\u5927\u53d4\u8bf4\uff0c\u8fd8\u6709\u4e00\u4e2a\u661f\u671f\u5c31\u8981\u4e0b\u5e02\u4e86\uff0c\u666e\u901a\uff0c\u4e14\u5403\u4e14\u73cd\u60dc\u3002<\/p>\n<p>\u5468\u672b\u4e24\u65e5\u9493\u8d39\u90fd\u662f100\u5143\uff0c\u7b2c\u4e00\u5929\u90a3\u4e2a\u9c7c\u5858\uff0c\u4e0d\u8c08\u4e86\uff0c\u96be\u9493\u7684\u4e0d\u5f97\u4e86\uff0c\u6536\u83b7\u51e0\u6761\u91ce\u751f\u6742\u9c7c\uff0c\u7b2c2\u5929\u7684\u5c31\u597d\u9493\u591a\u4e86\uff0c\u90fd\u662f\u957f\u6c5f\u9cca\uff08\u7fd8\u5634\u548c\u9cca\u9c7c\u6742\u4ea4\u54c1\u79cd\uff09\uff0c\u4e0d\u8fc7\u5927\u98ce\u5927\u96e8\u7684\uff0c\u505a\u9493\u4f53\u9a8c\u4e0d\u600e\u4e48\u597d\u3002<\/p>\n<p>\u8fd9\u5468\u7684\u8bdd\uff0c\u8ba1\u5212\u53bb\u5609\u5b9a\u7684\u62fe\u5149\u751f\u6001\u53bb\u9493\u6df7\u517b\uff0c\u5468\u65e5\u53bb\u58f9\u53f7\u9493\u573a\u53bb\u9493\u9ca4\u9c7c\uff0c\u987a\u4fbf\u8bd5\u7528\u51e0\u4e2a\u65b0\u9975\u6599\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202410\/2024-10-24_224837.jpeg\" width=\"138\" height=\"184\" alt=\"\u6e14\u83b7\u7167\u7247\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11387\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11387<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202410\/js-bird-cover.jpg\" width=\"160\" height=\"102\" alt=\"\u559c\u9e4a\u9e1f\u7c7b\u5360\u4f4d\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u4ecb\u7ecd\u51e0\u4e2aJS\u4e2d\u975e\u5e38\u96f7\u540c\u7684\u65b9\u6cd5\u6216\u8005\u8bed\u6cd5\uff0c\u4e0d\u77e5\u9053\u8bf8\u4f4d\u662f\u5426\u53ef\u77e5\u5176\u4e2d\u7684\u5dee\u5f02\uff1f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1887,1237,2008,903,1885,2007],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11387"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11387"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11387\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}