{"id":52716,"date":"2024-12-03T15:23:09","date_gmt":"2024-12-03T07:23:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52716\/"},"modified":"2024-12-03T15:23:09","modified_gmt":"2024-12-03T07:23:09","slug":"jquery%e4%b8%ad%e8%8e%b7%e5%8f%96%e5%af%b9%e8%b1%a1%e7%b1%bb%e5%9e%8b%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e4%bb%a3%e7%a0%81%ef%bc%8cclass2type-tostring-call-obj-%e7%9a%84%e4%bd%9c%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52716\/","title":{"rendered":"jQuery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0cclass2type[ toString.call( obj ) ]\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>jQuery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0cclass2type[ toString.call( obj ) ]\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p>\u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300ajQuery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0cclass2type[ toString.call( obj ) ]\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/17322816776740854d8e878.jpg\" class=\"aligncenter\" title=\"jQuery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0cclass2type[ toString.call( obj ) ]\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"jQuery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0cclass2type[ toString.call( obj ) ]\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>jquery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0ctostring.call(obj)\u8be6\u89e3<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5728jquery\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\u4e2d\uff0cclass2type[ tostring.call( obj ) ]\u8fd9\u4e00\u884c\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/p>\n<p><strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>var class2type = {};\nvar tostring = class2type.tostring;\n\nfunction totype( obj ) {\n    if ( obj == null ) {\n        return obj + \"\";\n    }\n\n    return typeof obj === \"object\" ?\n        class2type[ tostring.call( obj ) ] || \"object\" :\n        typeof obj;\n}<\/pre>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>class2type.tostring\u7b49\u540c\u4e8eobject.prototype.tostring\uff0c\u5b83\u7528\u4ee5\u83b7\u53d6\u5bf9\u8c61\u7684\u7c7b\u578b\u5b57\u7b26\u4e32\u3002<\/p>\n<p>tostring.call( obj )\u8c03\u7528\u4e86\u5bf9\u8c61\u7684tostring\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\u5b57\u7b26\u4e32\u3002\u5982\u201c[object object]\u201d\u3001\u201c[object array]\u201d\u3001\u201c[object function]\u201d\u7b49\u3002<\/p>\n<p>\u5728totype\u51fd\u6570\u4e2d\uff0ctostring.call( obj )\u83b7\u53d6\u7684\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u88ab\u7528\u4f5cclass2type\u5bf9\u8c61\u7684\u952e\u3002class2type\u5bf9\u8c61\u5b58\u50a8\u4e86\u4e0d\u540c\u7684\u7c7b\u578b\u5b57\u7b26\u4e32\u4e0e\u5176\u5bf9\u5e94\u7684\u7c7b\u578b\u540d\u79f0\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>class2type[\"[object Object]\"] = \"object\";\nclass2type[\"[object Array]\"] = \"array\";\nclass2type[\"[object Function]\"] = \"function\";<\/pre>\n<p>\u56e0\u6b64\uff0cclass2type[ tostring.call( obj ) ]\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\u540d\u79f0\u3002\u5b83\u7528\u4e8e\u786e\u5b9a\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5bf9\u5176\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300ajQuery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0cclass2type[ toString.call( obj ) ]\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u56e0\u8bbe\u5907\u8ba2\u8d2d\u5468\u671f\u8f83\u957f\uff0c\u8def\u7ef4\u5149\u7535\u7814\u53d1\u4e2d\u5fc3\u5efa\u8bbe\u9879\u76ee\u5efa\u8bbe\u671f\u5ef6\u671f1\u5e74\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>jQuery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0cclass2type[ toString.call( obj ) ]\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f \u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587\u300ajQuery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0cclass2type[ toString.call( obj ) ]\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\u300b\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60\u6587\u7ae0\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~ jquery\u4e2d\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\uff0ctostring.call(obj)\u8be6\u89e3 \u95ee\u9898\uff1a \u5728jquery\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u5b57\u7b26\u4e32\u7684\u4ee3\u7801\u4e2d\uff0cclass2type[ tostring.call( obj ) ]\u8fd9\u4e00\u884c\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f \u4ee3\u7801\uff1a var class2type = {}; var tostring = class2type.tostring; function totype( obj ) { if ( obj == null ) { return obj + &#8220;&#8221;; } return typeof obj === &#8220;object&#8221; ? class2type[ tostring.call( obj ) ] || [&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-52716","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52716","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=52716"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52716\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}