{"id":1395,"date":"2022-07-10T23:02:14","date_gmt":"2022-07-10T15:02:14","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1395"},"modified":"2023-02-16T14:41:29","modified_gmt":"2023-02-16T06:41:29","slug":"17-javascript-lei-xing-zhuan-huan","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1395","title":{"rendered":"17 &#8211; JavaScript \uff1a\u7c7b\u578b\u8f6c\u6362"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/07\/16574653672553.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/javascript-type-conversion-14eg\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/javascript-type-conversion-14eg<\/a><\/p>\n<h2><a id=\"%E8%BD%AC%E4%B8%BAboolean\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8f6c\u4e3a Boolean<\/h2>\n<p>JavaScript \u4e2d Boolean \u503c\u6709\u4e24\u4e2a\uff1a<code>true<\/code> \u548c <code>false<\/code>\u3002\u4f46\u662f\uff0cJavaScript \u8fd8\u4f1a\u628a\u7279\u5b9a\u7684\u503c\u89c6\u4e3a <code>truthy<\/code> \u548c <code>falsy<\/code>\u3002\u9664\u4e86 <code>0<\/code> \u3001<code>undefined<\/code> \u3001<code>&quot;&quot;<\/code>\u3001<code>false<\/code> \u548c <code>NaN<\/code> \u5176\u5b83\u503c\u90fd\u4e3a <code>truthy<\/code>\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53d6\u53cd\u64cd\u4f5c\u7b26 <code>!<\/code> \u5728 true \u548c false \u4e4b\u95f4\u6765\u56de\u5207\u6362\u3002\u8fd9\u79cd\u8f6c\u6362\u4e5f\u628a\u6570\u636e\u7c7b\u578b\u8f6c\u4e3a\u4e86 <code>boolean<\/code>\u3002<\/p>\n<pre><code class=\"language-plain_text\">const a = null;\nconst b = undefined;\nconst c = &quot;&quot;;\nconst d = 0;\n\nconsole.log(typeof a); \/\/ object\nconsole.log(typeof b); \/\/ undefined\nconsole.log(typeof c); \/\/ string\nconsole.log(typeof d); \/\/ number\n\nconst w = !a;\nconst x = !b;\nconst y = !c;\nconst z = !d;\n\nconsole.log(typeof w); \/\/ boolean\nconsole.log(typeof x); \/\/ boolean\nconsole.log(typeof y); \/\/ boolean\nconsole.log(typeof z); \/\/ boolean\n<\/code><\/pre>\n<p>\u4e0d\u4ec5\u628a\u7c7b\u578b\u8f6c\u4e3a\u4e86 boolean \uff0c\u8fd8\u6539\u53d8\u4e86\u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u4f60\u9700\u8981\u8f6c\u6362 boolean \u7c7b\u578b\u540c\u65f6\u4fdd\u6301\u5bf9\u5e94\u7684 <code>truthy<\/code> \u6216 <code>falsy<\/code> \u4f7f\u7528 <code>!!<\/code>\u3002<\/p>\n<pre><code class=\"language-plain_text\">const a = null;\nconst b = undefined;\nconst c = &quot;&quot;;\nconst d = 0;\n\nconsole.log(typeof a); \/\/ object\nconsole.log(typeof b); \/\/ undefined\nconsole.log(typeof c); \/\/ string\nconsole.log(typeof d); \/\/ number\n\nconst w = !!a;\nconst x = !!b;\nconst y = !!c;\nconst z = !!d;\n\nconsole.log(typeof w); \/\/ boolean\nconsole.log(typeof x); \/\/ boolean\nconsole.log(typeof y); \/\/ boolean\nconsole.log(typeof z); \/\/ boolean\n\n\/\/ Let's check if they are all false though and haven't switched to true!\n\nconsole.log(w); \/\/ false\nconsole.log(x); \/\/ false\nconsole.log(y); \/\/ false\nconsole.log(z); \/\/ false\n<\/code><\/pre>\n<h2><a id=\"%E8%BD%AC%E4%B8%BAstring\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8f6c\u4e3a String<\/h2>\n<p>\u4f7f\u7528 <code>toString()<\/code> \u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-plain_text\">const num = 7;\nconsole.log(typeof num); \/\/ number\nconst numString = num.toString();\nconsole.log(typeof numString); \/\/ string\n<\/code><\/pre>\n<p>\u6216\u4f7f\u7528\u4fbf\u6377\u65b9\u5f0f\u5728\u540e\u9762\u6dfb\u52a0 <code>&quot;&quot;<\/code>\u3002<\/p>\n<pre><code class=\"language-plain_text\">const num = 7;\nconsole.log(typeof num); \/\/ number\nconst numString = num + &quot;&quot;;\nconsole.log(typeof numString); \/\/ string\n<\/code><\/pre>\n<h2><a id=\"%E8%BD%AC%E4%B8%BAnumber\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8f6c\u4e3a Number<\/h2>\n<p><code>parseInt()<\/code> \u89e3\u6790\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e3a\u6574\u6570\uff0c\u4f60\u4f20\u9012\u7ed9\u5b83\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f5c\u4e3a\u57fa\u6570\u3002\u5b83\u6307\u5b9a\u4f7f\u7528\u54ea\u4e2a\u8fdb\u5236\u7c7b\u578b\uff1ahexadecimal (16), octal (8), or decimal (10)\u3002<\/p>\n<pre><code class=\"language-plain_text\">console.log(parseInt(&quot;0xF&quot;, 16)); \/\/ 15\nconsole.log(parseInt(&quot;321&quot;, 10)); \/\/ 321\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528\u4fbf\u6377\u65b9\u6cd5\u5728\u5b57\u7b26\u4e32\u4e4b\u524d\u6dfb\u52a0 <code>+<\/code>!<\/p>\n<pre><code class=\"language-plain_text\">console.log(+&quot;0xF&quot;); \/\/ 15\nconsole.log(+&quot;321&quot;); \/\/ 321\n<\/code><\/pre>\n<p>\u6709\u4e9b\u60c5\u51b5\u4e0b <code>+<\/code> \u53ef\u80fd\u88ab\u7528\u4e8e\u5b57\u7b26\u4e32\u94fe\u63a5\u3002\u5728\u90a3\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u6309\u4f4d\u8fd0\u7b97\u64cd\u4f5c\u7b26 <code>~<\/code> \u4e24\u6b21\uff1a<\/p>\n<pre><code class=\"language-plain_text\">console.log(~~&quot;0xF&quot;); \/\/ 15\nconsole.log(~~&quot;321&quot;); \/\/ 321\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/javascript-typ&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[189,190,188,187],"class_list":["post-1395","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-boolean","tag-number","tag-tostring","tag-187"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1395"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1395\/revisions"}],"predecessor-version":[{"id":1396,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1395\/revisions\/1396"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}