{"id":3216,"date":"2024-11-10T14:18:16","date_gmt":"2024-11-10T06:18:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3216\/"},"modified":"2024-11-10T14:18:16","modified_gmt":"2024-11-10T06:18:16","slug":"js%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%95%b0%e5%ad%97%e5%92%8c%e5%ad%97%e6%af%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3216\/","title":{"rendered":"js\u5982\u4f55\u5224\u65ad\u6570\u5b57\u548c\u5b57\u6bcd"},"content":{"rendered":"<blockquote><p>\n  javascript \u53ef\u4f7f\u7528 typeof \u8fd0\u7b97\u7b26\u5224\u65ad\u53d8\u91cf\u7c7b\u578b\uff0c\u5bf9\u4e8e\u6570\u5b57\u548c\u5b57\u6bcd\u7684\u5224\u65ad\u65b9\u6cd5\u5982\u4e0b\uff1a\u5224\u65ad\u6570\u5b57\uff1aif (typeof variable === &#8220;number&#8221;)\u5224\u65ad\u5b57\u6bcd\uff1aif (typeof variable === &#8220;string&#8221; &amp;&amp; variable.match(\/^[a-za-z]+$\/))\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110807211671066.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5224\u65ad\u6570\u5b57\u548c\u5b57\u6bcd\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5224\u65ad\u6570\u5b57\u548c\u5b57\u6bcd\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u5224\u65ad\u6570\u5b57\u548c\u5b57\u6bcd<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 typeof \u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5bf9\u4e8e\u6570\u5b57\u548c\u5b57\u6bcd\uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u522b\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u5224\u65ad\u6570\u5b57<\/strong><\/p>\n<pre>if (typeof variable === \"number\") {\n  \/\/ \u53d8\u91cf\u662f\u6570\u5b57\n} else {\n  \/\/ \u53d8\u91cf\u4e0d\u662f\u6570\u5b57\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5224\u65ad\u5b57\u6bcd<\/strong><\/p>\n<p>JavaScript \u6ca1\u6709\u4e13\u95e8\u7528\u4e8e\u5224\u65ad\u5b57\u6bcd\u7684\u7c7b\u578b\u8fd0\u7b97\u7b26\u3002\u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5224\u65ad\u53d8\u91cf\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u5b57\u6bcd\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre>if (typeof variable === \"string\" &amp;&amp; variable.match(\/^[a-zA-Z]+$\/)) {\n  \/\/ \u53d8\u91cf\u53ea\u5305\u542b\u5b57\u6bcd\n} else {\n  \/\/ \u53d8\u91cf\u4e0d\u53ea\u5305\u542b\u5b57\u6bcd\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f ^[a-zA-Z]+$ \u8868\u793a\u5b57\u7b26\u4e32\u5fc5\u987b\u4ee5\u4e00\u4e2a\u5b57\u6bcd\u5f00\u5934\uff0c\u5e76\u53ea\u5305\u542b\u5b57\u6bcd\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre>\/\/ \u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u6570\u5b57\nif (typeof myVariable === \"number\") {\n  console.log(\"myVariable \u662f\u6570\u5b57\");\n} else {\n  console.log(\"myVariable \u4e0d\u662f\u6570\u5b57\");\n}\n\n\/\/ \u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u53ea\u5305\u542b\u5b57\u6bcd\nif (typeof myVariable === \"string\" &amp;&amp; myVariable.match(\/^[a-zA-Z]+$\/)) {\n  console.log(\"myVariable \u53ea\u5305\u542b\u5b57\u6bcd\");\n} else {\n  console.log(\"myVariable \u4e0d\u53ea\u5305\u542b\u5b57\u6bcd\");\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5224\u65ad\u6570\u5b57\u548c\u5b57\u6bcd\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 \u53ef\u4f7f\u7528 typeof \u8fd0\u7b97\u7b26\u5224\u65ad\u53d8\u91cf\u7c7b\u578b\uff0c\u5bf9\u4e8e\u6570\u5b57\u548c\u5b57\u6bcd\u7684\u5224\u65ad\u65b9\u6cd5\u5982\u4e0b\uff1a\u5224\u65ad\u6570\u5b57\uff1aif (typeof variable === &#8220;number&#8221;)\u5224\u65ad\u5b57\u6bcd\uff1aif (typeof variable === &#8220;string&#8221; &amp;&amp; variable.match(\/^[a-za-z]+$\/)) \u5982\u4f55\u4f7f\u7528 JavaScript \u5224\u65ad\u6570\u5b57\u548c\u5b57\u6bcd \u5728 JavaScript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 typeof \u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5bf9\u4e8e\u6570\u5b57\u548c\u5b57\u6bcd\uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u522b\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u5224\u65ad\u6570\u5b57 if (typeof variable === &#8220;number&#8221;) { \/\/ \u53d8\u91cf\u662f\u6570\u5b57 } else { \/\/ \u53d8\u91cf\u4e0d\u662f\u6570\u5b57 } \u767b\u5f55\u540e\u590d\u5236 \u5224\u65ad\u5b57\u6bcd JavaScript \u6ca1\u6709\u4e13\u95e8\u7528\u4e8e\u5224\u65ad\u5b57\u6bcd\u7684\u7c7b\u578b\u8fd0\u7b97\u7b26\u3002\u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5224\u65ad\u53d8\u91cf\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u5b57\u6bcd\u6765\u5b9e\u73b0\uff1a if (typeof variable === &#8220;string&#8221; &amp;&amp; variable.match(\/^[a-zA-Z]+$\/)) { \/\/ \u53d8\u91cf\u53ea\u5305\u542b\u5b57\u6bcd } else { \/\/ [&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-3216","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3216","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=3216"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3216\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}