{"id":1503,"date":"2022-08-29T12:35:57","date_gmt":"2022-08-29T04:35:57","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1503"},"modified":"2022-08-29T12:45:54","modified_gmt":"2022-08-29T04:45:54","slug":"jian-ce-yi-ge-zhi-wei-shu-zu","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1503","title":{"rendered":"\u68c0\u6d4b\u4e00\u4e2a\u503c\u4e3a\u6570\u7ec4"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/\u5361\u901a\u63d2\u753b\u558a\u4f60\u56de\u6765\u4e0a\u73ed\u516c\u4f17\u53f7\u5c01\u9762\u9996\u56fe__2022-08-2912_45_21.jpeg\" alt=\"\u5361\u901a\u63d2\u753b\u558a\u4f60\u56de\u6765\u4e0a\u73ed\u516c\u4f17\u53f7\u5c01\u9762\u9996\u56fe__2022-08-29+12_45_21\" \/><\/p>\n<p>\u68c0\u6d4b\u4e00\u4e2a\u503c\u662f\u5426\u4e3a\u6570\u7ec4\u6709\u51e0\u79cd\u65b9\u6cd5\uff1f\u4eca\u5929\u6211\u4eec\u5c31\u6765\u804a\u804a\uff0c\u5728 JavaScript \u4e2d\u68c0\u6d4b\u6570\u636e\u7c7b\u578b\u7684\u4e00\u822c\u6709 typeof \u548c instanceof \u4e24\u4e2a\u8fd0\u7b97\u7b26\u3002\u4f46\u662f typeof \u5728\u68c0\u6d4b\u6570\u7ec4\u65f6\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">typeof []\ntypeof Array(3)\ntypeof new Array(3)\n<\/code><\/pre>\n<p>\u8fd4\u56de\u7684\u5168\u662f\uff1a&quot;object&quot;\uff0c\u6240\u4ee5\u4f7f\u7528 typeof \u662f\u4e0d\u80fd\u68c0\u6d4b\u7684\u3002\u5f53\u7136 instanceof \u662f\u53ef\u4ee5\u68c0\u6d4b\u7684\uff0c\u4f46\u5b83\u662f\u6709\u5c40\u9650\u6027\u7684\u540e\u9762\u4f1a\u8bf4\u660e\uff0c\u90a3\u9664\u4e86 instanceof \u8fd8\u6709\u522b\u7684\u65b9\u6cd5\u5417\uff1f\u8fd9\u6b21\u6211\u603b\u7ed3\u4e864\u79cd\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">Array.isArray(value)\nvalue instanceof Array \uff08 Object.getPrototypeOf(value) == Array.prototype \uff09\nvalue.constructor == Array\nObject.prototype.toString.call(value) == &quot;[object Array]&quot;\n<\/code><\/pre>\n<p>\u4e0b\u9762\u6211\u4eec\u4f9d\u6b21\u6765\u8bf4\u4e0b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">Array.isArray:\n<\/code><\/pre>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">Array.isArray(value)\n<\/code><\/pre>\n<p>\u5b9e\u4f8b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">\/\/ all following calls return true\nArray.isArray([]);\nArray.isArray([1]);\nArray.isArray(new Array());\nArray.isArray(new Array('a', 'b', 'c', 'd'));\nArray.isArray(new Array(3));\n\/\/ Little known fact: Array.prototype itself is an array:\nArray.isArray(Array.prototype); \n\n\/\/ all following calls return false\nArray.isArray();\nArray.isArray({});\nArray.isArray(null);\nArray.isArray(undefined);\nArray.isArray(17);\nArray.isArray('Array');\nArray.isArray(true);\nArray.isArray(false);\nArray.isArray(new Uint8Array(32));\nArray.isArray({ __proto__: Array.prototype });\n<\/code><\/pre>\n<p>\u517c\u5bb9\u6027\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16617479579093.jpg\" alt=\"\" \/><\/p>\n<p>\u8fd9\u4e5f\u662f\u73b0\u5728\u63a8\u8350\u7684\u68c0\u6d4b\u503c\u4e3a\u6570\u7ec4\u7684\u65b9\u6cd5\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">instanceof:\n<\/code><\/pre>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">object instanceof constructor\n<\/code><\/pre>\n<p>\u5b9e\u4f8b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const array = [1, 2, 3];\nconst object = { message: 'Hello!' };\nconst string = 'Hello!';\nconst empty = null;\n\narray  instanceof Array; \/\/ =&gt; true object instanceof Array; \/\/ =&gt; false\nstring instanceof Array; \/\/ =&gt; false\nempty  instanceof Array; \/\/ =&gt; false\n<\/code><\/pre>\n<p>\u517c\u5bb9\u6027\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16617479798229.jpg\" alt=\"\" \/><\/p>\n<p>instanceof \u7684\u539f\u7406\u5176\u5b9e\u5c31\u662f\u5bfb\u627e\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u662f\u5426\u51fa\u73b0\u5728\u88ab\u68c0\u6d4b\u503c\u7684\u539f\u578b\u94fe\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528 Object.getPrototypeOf \u83b7\u53d6\u539f\u578b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function C() {}\nfunction D() {}\n\nlet o = new C()\n\no instanceof C \/\/ true\nObject.getPrototypeOf(o) == C.prototype \/\/ true\n<\/code><\/pre>\n<p>\u4f46 instanceof \u6709\u4e2a\u5f0a\u7aef\uff0c\u5728\u591a\u4e0a\u4e0b\u6587\u4e5f\u5c31\u662f\u5b58\u5728 frames \u6216 windows \u65f6\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let iframeEl = document.createElement('iframe')\ndocument.body.appendChild(iframeEl)\niframeArray = window.frames[window.frames.length - 1].Array\n\nlet arr1 = new iframeArray(1,2,3,4)\nlet arr2 = new Array(1,2,3,4)\n\narr1 instanceof Array   \/\/ false\narr2 instanceof Array  \/\/ true\n<\/code><\/pre>\n<p>\u7a76\u5176\u539f\u56e0\u662f\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">Array.prototype != iframeArray.prototype\n<\/code><\/pre>\n<p><strong>constructor<\/strong>:<\/p>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">value.constructor\n<\/code><\/pre>\n<p>\u5b9e\u4f8b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">value.constructor == Array\n<\/code><\/pre>\n<p>\u540c\u6837\u548c instanceof \u6709\u7c7b\u4f3c\u7684\u95ee\u9898\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let iframeEl = document.createElement('iframe')\ndocument.body.appendChild(iframeEl)\niframeArray = window.frames[window.frames.length - 1].Array\n\nlet arr1 = new iframeArray(1,2,3,4)\nlet arr2 = new Array(1,2,3,4)\n\narr1.constructor == Array \/\/ false\n\nObject.prototype.toString\n<\/code><\/pre>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">Object.prototype.toString.call(value)\n<\/code><\/pre>\n<p>\u5b9e\u4f8b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">Object.prototype.toString.call(value) == &quot;[object Array]&quot;\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u8fd8\u662f\u6bd4\u8f83\u8001\u724c\u7684\u6ca1\u6709\u591a\u4e0a\u4e0b\u6587\u95ee\u9898\uff0c\u5728 Array.isArray \u6ca1\u6709\u7684\u60c5\u51b5\u4e0b\u5efa\u8bae\u9009\u62e9\u8be5\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">if (!Array.isArray) {\n  Array.isArray = function(arg) {\n    return Object.prototype.toString.call(arg) === '[object Array]';\n  };\n}\n<\/code><\/pre>\n<p>\u53e6\u5916\u591a\u8bf4\u4e00\u4e0b\u5176\u5b83\u5e93\u662f\u5982\u4f55\u68c0\u6d4b\u7684\uff1a<\/p>\n<p><strong>underscore.js<\/strong> \u7ecf\u5386\u4e86\u5982\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u7279\u5f81\u68c0\u6d4b\uff0c\u5e26\u6709 concat \u548c unshift \u65b9\u6cd5\u7684\u4e3a\u6570\u7ec4\u7c7b\u578b<\/li>\n<\/ol>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">var nativeIsArray = Array.isArray,\n\n_.isArray = nativeIsArray || function(obj) {\n  return !!(obj &amp;&amp; obj.concat &amp;&amp; obj.unshift);\n};\n<\/code><\/pre>\n<ol start=\"2\">\n<li>toString<\/li>\n<\/ol>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">var nativeIsArray = Array.isArray\n\nfunction tagTester(name) {\n  var tag = '[object ' + name + ']';\n  return function(obj) {\n    return toString.call(obj) === tag;\n  };\n}\nvar isArray = nativeIsArray || tagTester('Array');\n<\/code><\/pre>\n<p><strong>jquery<\/strong>\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">jQuery.each(&quot;Boolean Number String Function Array Date RegExp Object Error&quot;.split(&quot; &quot;), function(i, name) {\n  class2type[ &quot;[object &quot; + name + &quot;]&quot; ] = name.toLowerCase();\n});\n\u00a0\n$.type = function( obj ) {\n  ...\n  return typeof obj === &quot;object&quot; || typeof obj === &quot;function&quot; ?\n    class2type[ core_toString.call(obj) ] || &quot;object&quot; :\n    typeof obj;\n}\n\n\/\/ \u5224\u65ad\u6570\u7ec4\nisArray: Array.isArray || function( obj ) {\n  return jQuery.type(obj) === &quot;array&quot;;\n},\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u68c0\u6d4b\u4e00\u4e2a\u503c\u662f\u5426\u4e3a\u6570\u7ec4\u6709\u51e0\u79cd\u65b9\u6cd5\uff1f\u4eca\u5929\u6211\u4eec\u5c31\u6765\u804a\u804a\uff0c\u5728 JavaScript \u4e2d\u68c0\u6d4b\u6570\u636e\u7c7b\u578b\u7684\u4e00\u822c\u6709 &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[96,212],"class_list":["post-1503","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-array","tag-isarray"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1503"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1503\/revisions"}],"predecessor-version":[{"id":1506,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1503\/revisions\/1506"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}