{"id":3166,"date":"2024-11-10T12:55:37","date_gmt":"2024-11-10T04:55:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3166\/"},"modified":"2024-11-10T12:55:37","modified_gmt":"2024-11-10T04:55:37","slug":"js-%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3166\/","title":{"rendered":"js \u5982\u4f55\u5224\u65ad\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u8981\u5224\u65ad javascript \u4e2d\u7684\u503c\u662f\u5426\u4e3a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1aarray.isarray()\uff1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u76f4\u63a5\u6307\u793a\u503c\u662f\u5426\u4e3a\u6570\u7ec4\u3002instanceof\uff1a\u68c0\u67e5\u503c\u662f\u5426\u662f array \u7c7b\u7684\u5b9e\u4f8b\u3002object.prototype.tostring.call()\uff1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u503c\u7684\u7c7b\u578b\uff0c\u5bf9\u4e8e\u6570\u7ec4\u4e3a &#8216;[object array]&#8217;\u3002\u68c0\u67e5\u503c\u662f\u5426\u5177\u6709\u6570\u7ec4\u539f\u578b\u65b9\u6cd5\uff0c\u5982 push() \u548c length \u5c5e\u6027\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110805545580944.jpg\" class=\"aligncenter\" title=\"js \u5982\u4f55\u5224\u65ad\u6570\u7ec4\u63d2\u56fe\" alt=\"js \u5982\u4f55\u5224\u65ad\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7528 JavaScript \u5224\u65ad\u6570\u7ec4<\/strong><\/p>\n<p>JavaScript \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u4e3a\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. Array.isArray() \u65b9\u6cd5<\/strong><\/p>\n<pre>const isArray = Array.isArray(value);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>Array.isArray() \u65b9\u6cd5\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u7ed9\u5b9a\u503c\u662f\u5426\u4e3a\u6570\u7ec4\u3002<\/p>\n<p><strong>2. instanceof \u64cd\u4f5c\u7b26<\/strong><\/p>\n<pre>const isArray = value instanceof Array;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>instanceof \u64cd\u4f5c\u7b26\u68c0\u67e5\u7ed9\u5b9a\u503c\u662f\u5426\u662f\u6307\u5b9a\u7c7b\u7684\u5b9e\u4f8b\u3002\u5bf9\u4e8e\u6570\u7ec4\uff0c\u5b83\u68c0\u67e5\u8be5\u503c\u662f\u5426\u662f Array \u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<p><strong>3. Object.prototype.toString.call() \u65b9\u6cd5<\/strong><\/p>\n<pre>const isArray = Object.prototype.toString.call(value) === '[object Array]';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>Object.prototype.toString.call() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u5bf9\u4e8e\u6570\u7ec4\uff0c\u5b83\u8fd4\u56de &#8216;[object Array]&#8217;\u3002<\/p>\n<p><strong>4. \u68c0\u67e5\u503c\u662f\u5426\u5177\u6709\u6570\u7ec4\u539f\u578b\u65b9\u6cd5<\/strong><\/p>\n<pre>const hasPushMethod = value.hasOwnProperty('push');\nconst hasLengthProperty = value.hasOwnProperty('length');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6570\u7ec4\u5177\u6709\u72ec\u7279\u7684\u539f\u578b\u65b9\u6cd5\uff0c\u5982 push() \u548c length \u5c5e\u6027\u3002\u5982\u679c\u4e00\u4e2a\u503c\u5177\u6709\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u5b83\u5f88\u53ef\u80fd\u662f\u6570\u7ec4\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>Array.isArray() \u65b9\u6cd5\u662f\u9996\u9009\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u65e2\u6700\u5feb\u53c8\u6700\u53ef\u9760\u3002<\/li>\n<li>instanceof \u64cd\u4f5c\u7b26\u4e5f\u5f88\u5feb\uff0c\u4f46\u5982\u679c\u6570\u7ec4\u7684\u539f\u578b\u88ab\u91cd\u5199\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u9519\u8bef\u7684\u7ed3\u679c\u3002<\/li>\n<li>Object.prototype.toString.call() \u65b9\u6cd5\u662f\u6700\u901a\u7528\u4f46\u6700\u6162\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u68c0\u67e5\u539f\u578b\u65b9\u6cd5\u867d\u7136\u53ef\u9760\uff0c\u4f46\u4e0d\u5982\u524d\u4e24\u79cd\u65b9\u6cd5\u6709\u6548\u7387\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs \u5982\u4f55\u5224\u65ad\u6570\u7ec4\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>\u8981\u5224\u65ad javascript \u4e2d\u7684\u503c\u662f\u5426\u4e3a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1aarray.isarray()\uff1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u76f4\u63a5\u6307\u793a\u503c\u662f\u5426\u4e3a\u6570\u7ec4\u3002instanceof\uff1a\u68c0\u67e5\u503c\u662f\u5426\u662f array \u7c7b\u7684\u5b9e\u4f8b\u3002object.prototype.tostring.call()\uff1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u503c\u7684\u7c7b\u578b\uff0c\u5bf9\u4e8e\u6570\u7ec4\u4e3a &#8216;[object array]&#8217;\u3002\u68c0\u67e5\u503c\u662f\u5426\u5177\u6709\u6570\u7ec4\u539f\u578b\u65b9\u6cd5\uff0c\u5982 push() \u548c length \u5c5e\u6027\u3002 \u5982\u4f55\u7528 JavaScript \u5224\u65ad\u6570\u7ec4 JavaScript \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u4e3a\u6570\u7ec4\uff1a 1. Array.isArray() \u65b9\u6cd5 const isArray = Array.isArray(value); \u767b\u5f55\u540e\u590d\u5236 Array.isArray() \u65b9\u6cd5\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u7ed9\u5b9a\u503c\u662f\u5426\u4e3a\u6570\u7ec4\u3002 2. instanceof \u64cd\u4f5c\u7b26 const isArray = value instanceof Array; \u767b\u5f55\u540e\u590d\u5236 instanceof \u64cd\u4f5c\u7b26\u68c0\u67e5\u7ed9\u5b9a\u503c\u662f\u5426\u662f\u6307\u5b9a\u7c7b\u7684\u5b9e\u4f8b\u3002\u5bf9\u4e8e\u6570\u7ec4\uff0c\u5b83\u68c0\u67e5\u8be5\u503c\u662f\u5426\u662f Array \u7c7b\u7684\u5b9e\u4f8b\u3002 3. Object.prototype.toString.call() \u65b9\u6cd5 const isArray = Object.prototype.toString.call(value) === &#8216;[object Array]&#8217;; \u767b\u5f55\u540e\u590d\u5236 Object.prototype.toString.call() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u5bf9\u4e8e\u6570\u7ec4\uff0c\u5b83\u8fd4\u56de &#8216;[object Array]&#8217;\u3002 [&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-3166","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3166","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=3166"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3166\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}