{"id":417,"date":"2022-05-09T13:40:40","date_gmt":"2022-05-09T05:40:40","guid":{"rendered":"http:\/\/gjweb.top\/?p=417"},"modified":"2022-05-09T13:40:41","modified_gmt":"2022-05-09T05:40:41","slug":"js-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e6%a3%80%e6%9f%a5%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=417","title":{"rendered":"JS \u6570\u636e\u7c7b\u578b\u68c0\u67e5\u539f\u7406"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"\u6570\u636e\u7c7b\u578b\u68c0\u6d4b\u7684\u65b9\u6cd5\">\u6570\u636e\u7c7b\u578b\u68c0\u6d4b\u7684\u65b9\u6cd5<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>typeof : \u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u5305\u542b\u5bf9\u5e94\u7684\u6570\u5b57\u7c7b\u578b<ul><li>typeof \u53ea\u80fd\u68c0\u6d4b\u539f\u59cb\u7684\u6570\u636e\u7c7b\u578b<\/li><li>typeof null \u4f1a\u8fd4\u56de object<\/li><li>typeof new Number(0) \u4f1a\u8fd4\u56de object<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>let arr = &#91;];<br>typeof arr \/\/ object<br>typeof \"123\" \/\/ string <br>typeof new Number(0) \/\/ object<br>typeof null \/\/ object<br>typeof undefined \/\/ undefined<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul class=\"wp-block-list\"><li>[\u5b9e\u4f8b] instanceof [\u6784\u9020\u51fd\u6570]<ul><li>\u5f0a\u7aef<ul><li>\u672c\u8eab\u7684\u4f5c\u7528\u7684\u662f\u7528\u6765\u68c0\u67e5\u5f53\u524d\u5b9e\u4f8b\u662f\u5426\u5c5e\u4e8e\u8fd9\u4e2a\u7c7b\uff0c\u4e34\u65f6\u5145\u5f53\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u4f1a\u51fa\u73b0\u8bf8\u591a\u95ee\u9898<\/li><li>instanceof \u53ea\u80fd\u68c0\u67e5\u5f15\u7528\u7c7b\u578b<\/li><\/ul><\/li><li>\u8fd0\u884c\u539f\u7406<ul><li>\u9996\u5148\u6309\u7167 \u6784\u9020\u51fd\u6570<strong><a href=\"%5B%E5%AE%9E%E4%BE%8B%5D%20\">Symbol.hasInstance<\/a><\/strong>\uff0c\u5982\u679c\u5b58\u5728\u8fd9\u4e2a\u5c5e\u6027\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u5c31\u662f\u6700\u540e\u7684\u68c0\u67e5\u7ed3\u679c<\/li><li>\u5982\u679c\u4e0d\u5b58\u5728\u4e0a\u9762\u7684\u5c5e\u6027\u5219\u4f1a\u67e5\u627e\u5f53\u524d \u5b9e\u4f8b \u7684\u539f\u578b\u94fe\uff08\u4e00\u76f4\u5230 Object.prototype \uff0c\u5982\u679c\u67e5\u627e\u4e2d\u9014\uff0c\u627e\u5230\u7684\u67d0\u4e2a\u539f\u578b\u7b49\u4e8e\u3010\u6784\u9020\u51fd\u6570\u3011\u7684\u539f\u578b\uff0c\u5219\u8fd4\u56de\u7ed3\u679c\u662f true<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>let arr = &#91;];<br>arr instanceof Object \/\/ true<br>arr instanceof Array \/\/ true<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u624b\u5199\u5b9e\u73b0-instanceof\">\u624b\u5199\u5b9e\u73b0 instanceOf<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function instanceOf(){<br> &nbsp; &nbsp;\/\/ \u6570\u636e\u683c\u5f0f\u51c6\u786e\u6027\u6821\u9a8c<br> &nbsp; &nbsp;if (Ctor == null) throw new TypeError(\"Right-hand side of 'instanceof' is not an object\");<br> &nbsp; &nbsp;if (!Ctor.prototype) throw new TypeError(\"Function has non-object prototype 'undefined' in instanceof check\");<br> &nbsp; &nbsp;if (typeof Ctor !== \"function\") throw new TypeError(\"Right-hand side of 'instanceof' is not callable\");<br>\u200b<br> &nbsp; &nbsp;\/\/ \u539f\u59cb\u7c7b\u578b\u76f4\u63a5\u5ffd\u7565<br> &nbsp; &nbsp;if (obj == null) return false;<br> &nbsp; &nbsp;if (!\/^(object|function)$\/.test(typeof obj)) return false;<br>\u200b<br> &nbsp; &nbsp;\/\/ \u5148\u68c0\u6d4b\u662f\u5426\u6709 Symbol.hasInstance \u8fd9\u4e2a\u5c5e\u6027<br> &nbsp; &nbsp;if (typeof Ctor&#91;Symbol.hasInstance] === \"function\") return Ctor&#91;Symbol.hasInstance](obj);<br>\u200b<br> &nbsp; &nbsp;\/\/ \u6700\u540e\u624d\u4f1a\u6309\u7167\u539f\u578b\u94fe\u8fdb\u884c\u5904\u7406<br> &nbsp; &nbsp;let prototype = Object.getPrototypeOf(obj);<br> &nbsp; &nbsp;while (prototype) {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (prototype === Ctor.prototype) return true;<br> &nbsp; &nbsp; &nbsp; &nbsp;prototype = Object.getPrototypeOf(prototype);<br> &nbsp;  }<br> &nbsp; &nbsp;return false;<br>}<br>let res = instance_of(&#91;],Array); \/\/ true<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5bf9\u8c61].constructor === [\u6784\u9020\u51fd\u6570]<ul><li>\u5f0a\u7aef<ul><li>\u672c\u8eab\u4f5c\u7528\u662f\u83b7\u53d6\u5bf9\u8c61\u6784\u9020\u51fd\u6570<\/li><li>constructor \u672c\u8eab\u662f\u5141\u8bb8\u968f\u610f\u66f4\u6539\u7684\uff0c\u68c0\u6d4b\u7ed3\u679c\u662f\u4e0d\u51c6\u786e\u7684<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul class=\"wp-block-list\"><li>Object.prototype.toString.call(value)<ul><li>\u6ca1\u6709\u4efb\u4f55\u7f3a\u70b9<\/li><li>\u539f\u7406<ul><li>\u9996\u5148\u627e\u5230 Object.prototyoe.toString\u65b9\u6cd5\uff0c\u628a toString \u6267\u884c\u4e4b\u540e\uff0c\u8ba9\u65b9\u6cd5\u4e2d\u7684this\u53d8\u4e3a\u8981\u68c0\u6d4b\u7684\u8fd9\u4e2a\u503c\uff0ctoString\u5185\u90e8\u4f1a\u5bf9\u5e94this\uff08\u68c0\u6d4b\u8fd9\u4e2a\u503c\uff09\u7684\u6570\u636e\u7c7b\u578b\u4fe1\u606f \u201c [ object ? ] \u201d<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[<strong>\u6b64\u6587\u7ae0\u53c2\u8003 \u300a\u73e0\u5cf0\u524d\u7aef\u9ad8\u7ea7\u4f53\u7cfb\u8bfe\u300b\u200b<\/strong>] &nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u7c7b\u578b\u68c0\u6d4b\u7684\u65b9\u6cd5 typeof : \u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u5305\u542b\u5bf9\u5e94\u7684\u6570\u5b57\u7c7b\u578b typeof \u53ea\u80fd\u68c0\u6d4b\u539f\u59cb\u7684\u6570\u636e\u7c7b\u578b t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=417"}],"version-history":[{"count":2,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":419,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions\/419"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}