{"id":158,"date":"2022-08-21T21:16:19","date_gmt":"2022-08-21T13:16:19","guid":{"rendered":"http:\/\/weboob.top\/blog\/?p=158"},"modified":"2022-08-21T21:16:19","modified_gmt":"2022-08-21T13:16:19","slug":"typeof-%e5%92%8c-instanceof-%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"http:\/\/blog.weboob.top\/?p=158","title":{"rendered":"typeof \u548c instanceof \u89e3\u6790"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.jianshu.com\/p\/aa6b14e4226a\">\u53c2\u8003\u6587\u732e<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E4%B8%80%E5%89%8D%E5%AF%BC%E7%9F%A5%E8%AF%86\">\u4e00\u3001\u524d\u5bfc\u77e5\u8bc6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">javascript\u4e2d\u7684\u5185\u7f6e<a href=\"http:\/\/weboob.top\/blog\/?p=110\">\u6570\u636e\u7c7b\u578b<\/a>\u6709\u4ee5\u4e0b\u51e0\u5927\u7c7b<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>ES5\u4e2d\u7c7b\u578b<\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/* \u57fa\u672c\u6570\u636e\u7c7b\u578b *\/<\/em>\n\u5b57\u7b26\u4e32: string\n\u6570\u5b57\uff1anumber\n\u5e03\u5c14\u503c\uff1aboolean\n<em>\/* \u7279\u6b8a\u503c *\/<\/em>\n\u672a\u5b9a\u4e49: undefined\n\u7a7a\u503c: null\n<em>\/* \u590d\u5408\u7c7b\u578b *\/<\/em>\n\u5bf9\u8c61\uff1aobject(Array, Function, {}, RegExp)\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u540e\u671f\u65b0\u589e<\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>- symbol(ES6\u65b0\u589e)\n- BigInt(ES2020)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E4%BA%8Ctypeof-%E5%8E%9F%E7%90%86\">\u4e8c\u3001typeof \u539f\u7406<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9664\u5bf9\u8c61\u7c7b\u578b\u5176\u4ed6\u7684\u90fd\u79f0\u4e3a\u201c\u57fa\u672c\u7c7b\u578b\u201d<\/strong>\uff0ctypeof\u7684\u8fd4\u56de\u503c\u4e3a\u5404\u81ea\u7684\u7c7b\u578b\u503c\u3002\u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0ctypeof null\u8fd4\u56deobject\uff0ctypeof function\uff0c\u8fd4\u56defunction\u3002\u540e\u9762\u4f1a\u4ecb\u7ecd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * typeof\u5b9e\u4f8b\n *\/<\/em>\nconsole.log(\"---- basic type ----\");\nconsole.log(\"typeof '123' -&gt; \" + typeof '123');\nconsole.log(\"typeof 123 -&gt; \" + typeof 123);\nconsole.log(\"typeof true -&gt; \" + typeof true);\nconsole.log(\"typeof null -&gt; \" + typeof null);\nconsole.log(\"typeof undefined -&gt; \" + typeof undefined);\nconsole.log(\"---- object ----\");\nconsole.log(\"typeof {} -&gt; \" + typeof {});\nconsole.log(\"typeof function(){} -&gt; \" + typeof function (){});\nconsole.log(\"typeof &#91;] -&gt; \" + typeof &#91;]);\nconsole.log(\"typeof \/reg\/ig -&gt; \" + typeof \/reg\/ig);\nconsole.log(\"---- \u65b0\u7c7b\u578b ----\");\nconsole.log(\"typeof Symbol() -&gt; \" + typeof Symbol());\nconsole.log(\"typeof 123n -&gt; \" + typeof 123n);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---- basic type ----\ntypeof '123' -&gt; string\ntypeof 123 -&gt; number\ntypeof true -&gt; boolean\ntypeof null -&gt; object\ntypeof undefined -&gt; undefined\n---- object ----\ntypeof {} -&gt; object\ntypeof function(){} -&gt; function\ntypeof &#91;] -&gt; object\ntypeof \/reg\/ig -&gt; object\n---- \u65b0\u7c7b\u578b ----\ntypeof Symbol() -&gt; symbol\ntypeof 123n -&gt; bigint\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u5bf9\u8c61\u5305\u542b<code>[[call]]<\/code>\uff0c\u51fd\u6570\u4e5f\u88ab\u79f0\u4e3a<code>\u53ef\u6267\u884c\u5bf9\u8c61<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u6709typeof\u4e3aobject\u7684\u5bf9\u8c61\u90fd\u5305\u542b\u5185\u90e8\u5c5e\u6027<code>[[class]]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>Object.prototype.toString(...)<\/code>\u6765\u67e5\u770b\u8fd9\u4e2a\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * Object\u5185\u90e8\u5206\u7c7b&#91;&#91;class]] \u793a\u4f8b\n *\/<\/em>\nconsole.log(\"---- &#91;&#91;class]] ----\");\nconsole.log(\"Object.prototype.toString.call({}) -&gt; \" + Object.prototype.toString.call({}));\nconsole.log(\"Object.prototype.toString.call(&#91;]) -&gt; \" + Object.prototype.toString.call(&#91;]));\nconsole.log(\"Object.prototype.toString.call(function(){}) -&gt; \"\n+ Object.prototype.toString.call(function (){}));\nconsole.log(\"Object.prototype.toString.call(\/reg\/ig) -&gt; \" + Object.prototype.toString.call(\/reg\/ig));\n\n<em>\/\/ \u8f93\u51fa\u5185\u5bb9\u5982\u4e0b<\/em>\n<em>\/\/ ---- &#91;&#91;class]] ----<\/em>\n<em>\/\/ Object.prototype.toString.call({})&#91;object Object]<\/em>\n<em>\/\/ Object.prototype.toString.call(&#91;])&#91;object Array]<\/em>\n<em>\/\/ Object.prototype.toString.call(function(){})&#91;object Function]<\/em>\n<em>\/\/ Object.prototype.toString.call(\/reg\/ig) -&gt; &#91;object RegExp]<\/em>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">typeof \u548c instanceof \u90fd\u662f\u8fd0\u7b97\u7b26\uff0c\u4e0d\u662f\u51fd\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"%E6%A3%80%E6%B5%8B%E6%9C%BA%E5%88%B6\">\u68c0\u6d4b\u673a\u5236<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u5728JS\u5e95\u5c42\u8868\u793a\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u4e0d\u4e00\u6837\u3002\u4e3b\u8981\u533a\u5206\u5728<code>\u4f4e\u4e09\u4f4d<\/code>(\u53f3\u4fa7\u6570\u636e)\u3002\u5728\u7b2c\u4e00\u7248\u7684js\u8bbe\u8ba1\u4e2d\uff0c\u4f7f\u752832\u4f4d\u4f5c\u4e3a\u5b58\u50a8\u5355\u5143\uff0c\u5e76\u4f7f\u7528\u4f4e\u4e09\u4f4d\uff081-3\u4f4d\uff09\u8868\u793a\u503c\u7684\u7c7b\u578b<a href=\"http:\/\/t.zoukankan.com\/tkqq000-p-14846275.html\">\uff08\u53c2\u8003\u6587\u732e\uff09<\/a><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>000\uff1aObject\u7c7b\u578b\uff0c\u540e\u7eed\u4f4d\u6570\u7528\u4e8e\u5b58\u50a8\u6307\u5411\u5bf9\u8c61\u7684\u5f15\u7528\u3002<strong>\u800cnull\u7684\u540e31\u4f4d\u5168\u662f0\uff0c\u7528\u4e8e\u8868\u793a\u65e0\u5f15\u7528\uff0c\u4e5f\u5c31\u662f\u7a7a\u5bf9\u8c61<\/strong>\u3002<\/li><li>1\uff1aint\u7c7b\u578b\uff0c\u540e\u7eed\u4f4d\u6570\u5b58\u50a8\u4e00\u4e2a31\u4f4d\u7684\u6709\u7b26\u53f7\u6574\u6570\u3002<\/li><li>010\uff1adouble\u7c7b\u578b\u3002\u540e\u7eed\u4f4d\u6570\u5b58\u50a8\u4e00\u4e2a\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002<\/li><li>100\uff1astring\u7c7b\u578b\u3002<\/li><li>110\uff1a\u5e03\u5c14\u503c<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e5f\u5c31\u662f\u89e3\u91ca\u4e86\u524d\u9762\u63d0\u5230\u7684<code>typeof null -&gt; object<\/code>\u95ee\u9898<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E4%B8%89instanceof-%E5%8E%9F%E7%90%86\">\u4e09\u3001instanceof \u539f\u7406<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">instanceof\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * instanceof\u793a\u4f8b\n * VM instanceof Constructor\n *\/<\/em>\nconsole.log(\"&#91;] instanceof Array -&gt; \" + (&#91;] instanceof Array)); <em>\/\/ true<\/em>\nconsole.log(\"&#91;] instanceof Object -&gt; \" + (&#91;] instanceof Object)); <em>\/\/ true<\/em>\nconsole.log(\"&#91;] instanceof RegExp -&gt; \" + (&#91;] instanceof RegExp)); <em>\/\/ false<\/em>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">instanceof\u662f\u901a\u8fc7<a href=\"http:\/\/weboob.top\/blog\/?p=152\">\u539f\u578b\u94fe<\/a>\u68c0\u6d4b\uff0c\u68c0\u6d4b\u6784\u9020\u51fd\u6570\u7684&nbsp;<code>prototype<\/code>&nbsp;\u5c5e\u6027\u662f\u5426\u51fa\u73b0\u5728\u67d0\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u7684\u539f\u578b\u94fe\u4e0a\u3002instanceof \u53ef\u4ee5\u68c0\u6d4b\u67d0\u4e2a\u5b9e\u4f8b\u662f\u5426\u4e3a\u67d0\u4e2a\u6784\u9020\u51fd\u6570\u7684\u5b9e\u4f8b\u3002\u53ef\u4ee5\u8d8a\u7ea7\u68c0\u6d4b\uff08\u7236\u7c7b\u3001\u7236\u7236\u7c7b\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>instanceof \u53ea\u9002\u7528\u4e8e\u5bf9\u8c61\u7c7b\u578b\u7684\u68c0\u6d4b<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u6587\u732e \u4e00\u3001\u524d\u5bfc\u77e5\u8bc6 javascript\u4e2d\u7684\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u6709\u4ee5\u4e0b\u51e0\u5927\u7c7b ES5\u4e2d\u7c7b\u578b \u540e\u671f\u65b0\u589e \u4e8c\u3001type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=158"}],"version-history":[{"count":0,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts\/158\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}