{"id":3170,"date":"2024-11-10T12:54:27","date_gmt":"2024-11-10T04:54:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3170\/"},"modified":"2024-11-10T12:54:27","modified_gmt":"2024-11-10T04:54:27","slug":"js%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%98%af%e4%b8%80%e4%b8%aa%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3170\/","title":{"rendered":"js\u5982\u4f55\u5224\u65ad\u662f\u4e00\u4e2a\u5bf9\u8c61"},"content":{"rendered":"<blockquote><p>\n  javascript\u5224\u65ad\u53d8\u91cf\u662f\u5426\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\uff1atypeof \u8fd0\u7b97\u7b26\uff1a\u5982\u679c\u53d8\u91cf\u7c7b\u578b\u4e3a&#8221;object&#8221;\uff0c\u5219\u4e3a\u5bf9\u8c61\u3002instanceof \u8fd0\u7b97\u7b26\uff1a\u5982\u679c\u53d8\u91cf\u7ee7\u627f\u81ea object \u7c7b\uff0c\u5219\u4e3a\u5bf9\u8c61\u3002object.prototype.tostring() \u65b9\u6cd5\uff1a\u5982\u679c\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u4e3a&#8221;[object object]&#8221;\uff0c\u5219\u4e3a\u5bf9\u8c61\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110806040079285.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5224\u65ad\u662f\u4e00\u4e2a\u5bf9\u8c61\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5224\u65ad\u662f\u4e00\u4e2a\u5bf9\u8c61\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7528 JavaScript \u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u4e00\u4e2a\u5bf9\u8c61<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u4e00\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4e4b\u4e00\uff1a<\/p>\n<p><strong>1. typeof \u8fd0\u7b97\u7b26<\/strong><\/p>\n<pre>if (typeof variable === \"object\") {\n  \/\/ variable \u662f\u4e00\u4e2a\u5bf9\u8c61\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. instanceof \u8fd0\u7b97\u7b26<\/strong><\/p>\n<pre>if (variable instanceof Object) {\n  \/\/ variable \u662f\u4e00\u4e2a\u5bf9\u8c61\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. Object.prototype.toString() \u65b9\u6cd5<\/strong><\/p>\n<pre>if (Object.prototype.toString.call(variable) === \"[object Object]\") {\n  \/\/ variable \u662f\u4e00\u4e2a\u5bf9\u8c61\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>typeof \u8fd0\u7b97\u7b26\u4e5f\u4f1a\u5c06 null \u5224\u65ad\u4e3a\u5bf9\u8c61\uff0c\u56e0\u6b64\u5982\u679c\u9700\u8981\u6392\u9664 null\uff0c\u8bf7\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<li>instanceof \u8fd0\u7b97\u7b26\u4ec5\u9002\u7528\u4e8e\u7ee7\u627f\u81ea Object \u7c7b\u7684\u5bf9\u8c61\u3002<\/li>\n<li>Object.prototype.toString() \u65b9\u6cd5\u8fd4\u56de\u53d8\u91cf\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u9700\u8981\u6bd4\u8f83\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u662f\u5426\u4e0e &#8220;[object Object]&#8221; \u5339\u914d\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5224\u65ad\u662f\u4e00\u4e2a\u5bf9\u8c61\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>javascript\u5224\u65ad\u53d8\u91cf\u662f\u5426\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\uff1atypeof \u8fd0\u7b97\u7b26\uff1a\u5982\u679c\u53d8\u91cf\u7c7b\u578b\u4e3a&#8221;object&#8221;\uff0c\u5219\u4e3a\u5bf9\u8c61\u3002instanceof \u8fd0\u7b97\u7b26\uff1a\u5982\u679c\u53d8\u91cf\u7ee7\u627f\u81ea object \u7c7b\uff0c\u5219\u4e3a\u5bf9\u8c61\u3002object.prototype.tostring() \u65b9\u6cd5\uff1a\u5982\u679c\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u4e3a&#8221;[object object]&#8221;\uff0c\u5219\u4e3a\u5bf9\u8c61\u3002 \u5982\u4f55\u7528 JavaScript \u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u4e00\u4e2a\u5bf9\u8c61 \u5728 JavaScript \u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u4e00\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4e4b\u4e00\uff1a 1. typeof \u8fd0\u7b97\u7b26 if (typeof variable === &#8220;object&#8221;) { \/\/ variable \u662f\u4e00\u4e2a\u5bf9\u8c61 } \u767b\u5f55\u540e\u590d\u5236 2. instanceof \u8fd0\u7b97\u7b26 if (variable instanceof Object) { \/\/ variable \u662f\u4e00\u4e2a\u5bf9\u8c61 } \u767b\u5f55\u540e\u590d\u5236 3. Object.prototype.toString() \u65b9\u6cd5 if (Object.prototype.toString.call(variable) === &#8220;[object Object]&#8221;) { \/\/ variable \u662f\u4e00\u4e2a\u5bf9\u8c61 } \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a [&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-3170","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3170","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=3170"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3170\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}