{"id":222,"date":"2023-02-23T06:32:19","date_gmt":"2023-02-22T22:32:19","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=222"},"modified":"2023-04-30T15:24:00","modified_gmt":"2023-04-30T07:24:00","slug":"js-type-conversion-mandatory-and-automatic-rules","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/23\/js-type-conversion-mandatory-and-automatic-rules\/","title":{"rendered":"JS\u7c7b\u578b\u8f6c\u6362\uff08\u5f3a\u5236\u548c\u81ea\u52a8\u7684\u89c4\u5219\uff09"},"content":{"rendered":"<h2>\u663e\u5f0f\u8f6c\u6362<\/h2>\n<p>\u901a\u8fc7\u624b\u52a8\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0cJavascript\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u8f6c\u578b\u51fd\u6570\uff1a<\/p>\n<ul>\n<li>\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\uff1aNumber(mix)\u3001parseInt(string,radix)\u3001parseFloat(string)<\/li>\n<li>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\uff1atoString(radix)\u3001String(mix)<\/li>\n<li>\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\uff1aBoolean(mix)<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h3>Number(mix)\u51fd\u6570<\/h3>\n<p>\u53ef\u4ee5\u5c06\u4efb\u610f\u7c7b\u578b\u7684\u53c2\u6570mix\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\u3002\u5176\u89c4\u5219\u4e3a\uff1a<\/p>\n<p>\uff081\uff09\u5982\u679c\u662f\u5e03\u5c14\u503c\uff0ctrue\u548cfalse\u5206\u522b\u88ab\u8f6c\u6362\u4e3a1\u548c0<br \/>\n\uff082\uff09\u5982\u679c\u662f\u6570\u5b57\u503c\uff0c\u8fd4\u56de\u672c\u8eab\u3002<br \/>\n\uff083\uff09\u5982\u679c\u662fnull\uff0c\u8fd4\u56de0.<br \/>\n\uff084\uff09\u5982\u679c\u662fundefined\uff0c\u8fd4\u56deNaN\u3002<br \/>\n\uff085\uff09\u5982\u679c\u662f\u5b57\u7b26\u4e32\uff0c\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a<\/p>\n<ul>\n<li>1\u3001\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b\u6570\u5b57\uff0c\u5219\u5c06\u5176\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff08\u5ffd\u7565\u524d\u5bfc0\uff09<\/li>\n<li>2\u3001\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6709\u6548\u7684\u6d6e\u70b9\u683c\u5f0f\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u503c\uff08\u5ffd\u7565\u524d\u5bfc0\uff09<\/li>\n<li>3\u3001\u5982\u679c\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a0<\/li>\n<li>4\u3001\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u4ee5\u4e0a\u683c\u5f0f\uff0c\u5219\u5c06\u5176\u8f6c\u6362\u4e3aNaN<br \/>\n\uff086\uff09\u5982\u679c\u662f\u5bf9\u8c61\uff0c\u5219\u8c03\u7528\u5bf9\u8c61\u7684valueOf()\u65b9\u6cd5\uff0c\u7136\u540e\u4f9d\u636e\u524d\u9762\u7684\u89c4\u5219\u8f6c\u6362\u8fd4\u56de\u7684\u503c\u3002\u5982\u679c\u8f6c\u6362\u7684\u7ed3\u679c\u662fNaN\uff0c\u5219\u8c03\u7528\u5bf9\u8c61\u7684toString()\u65b9\u6cd5\uff0c\u518d\u6b21\u4f9d\u7167\u524d\u9762\u7684\u89c4\u5219\u8f6c\u6362\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u503c\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u8868\u5217\u51fa\u4e86\u5bf9\u8c61\u7684valueOf()\u7684\u8fd4\u56de\u503c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u5bf9\u8c61<\/th>\n<th style=\"text-align: center;\">\u8fd4\u56de\u503c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">Array<\/td>\n<td style=\"text-align: center;\">\u6570\u7ec4\u7684\u5143\u7d20\u88ab\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e9b\u5b57\u7b26\u4e32\u7531\u9017\u53f7\u5206\u9694\uff0c\u8fde\u63a5\u5728\u4e00\u8d77<br \/>\u5176\u64cd\u4f5c\u4e0e Array.toString \u548c Array.join \u65b9\u6cd5\u76f8\u540c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Boolean<\/td>\n<td style=\"text-align: center;\">Boolean\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Date<\/td>\n<td style=\"text-align: center;\">\u5b58\u50a8\u7684\u65f6\u95f4\u662f\u4ece1970\u5e741\u67081\u65e5\u5348\u591c\u5f00\u59cb\u8ba1\u7684\u6beb\u79d2\u6570UTC<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Function<\/td>\n<td style=\"text-align: center;\">\u51fd\u6570\u672c\u8eab<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Number<\/td>\n<td style=\"text-align: center;\">\u6570\u5b57\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Object<\/td>\n<td style=\"text-align: center;\">\u5bf9\u8c61\u672c\u8eab\uff0c\u8fd9\u662f\u9ed8\u8ba4\u60c5\u51b5<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">String<\/td>\n<td style=\"text-align: center;\">\u5b57\u7b26\u4e32\u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-javascript\">> Number(&quot;Hello AppBlog.CN!&quot;)\nNaN\n> Number(&quot;0x8&quot;)\n8\n> Number(&quot;&quot;)\n0\n> Number(&quot;020dd&quot;)\nNaN\n> Number(&quot;050&quot;)\n50\n> Number(true)\n1\n><\/code><\/pre>\n<h3>parseInt(string, radix)\u51fd\u6570<\/h3>\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u7684\u6570\u503c\u3002\u5b83\u4e5f\u6709\u4e00\u5b9a\u7684\u89c4\u5219\uff1a<\/p>\n<p>\uff081\uff09\u5ffd\u7565\u5b57\u7b26\u4e32\u524d\u9762\u7684\u7a7a\u683c\uff0c\u76f4\u81f3\u627e\u5230\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26<br \/>\n\uff082\uff09\u5982\u679c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e0d\u662f\u6570\u5b57\u7b26\u53f7\u6216\u8005\u8d1f\u53f7\uff0c\u8fd4\u56deNaN<br \/>\n\uff083\uff09\u5982\u679c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u6570\u5b57\uff0c\u5219\u7ee7\u7eed\u89e3\u6790\u76f4\u81f3\u5b57\u7b26\u4e32\u89e3\u6790\u5b8c\u6bd5\u6216\u8005\u9047\u5230\u4e00\u4e2a\u975e\u6570\u5b57\u7b26\u53f7\u4e3a\u6b62<br \/>\n\uff084\uff09\u5982\u679c\u4e0a\u6b65\u89e3\u6790\u7684\u7ed3\u679c\u4ee50\u5f00\u5934\uff0c\u5219\u5c06\u5176\u5f53\u4f5c\u516b\u8fdb\u5236\u6765\u89e3\u6790\uff1b\u5982\u679c\u4ee5x\u5f00\u5934\uff0c\u5219\u5c06\u5176\u5f53\u4f5c\u5341\u516d\u8fdb\u5236\u6765\u89e3\u6790<br \/>\n\uff085\uff09\u5982\u679c\u6307\u5b9aradix\u53c2\u6570\uff0c\u5219\u4ee5radix\u4e3a\u57fa\u6570\u8fdb\u884c\u89e3\u6790<\/p>\n<pre><code class=\"language-javascript\">> parseInt(&quot;Hello AppBlog.CN!&quot;)\nNaN\n> parseInt(&quot;0x8&quot;)\n8\n> parseInt(&quot;&quot;)\nNaN\n> parseInt(&quot;020dd&quot;)\n20\n> parseInt(&quot;050&quot;)\n50\n> parseInt(&quot;22.5&quot;)\n22\n><\/code><\/pre>\n<h3>parseFloat(string)\u51fd\u6570<\/h3>\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u7684\u6570\u503c\u3002\u89c4\u5219\u4e0eparseInt\u57fa\u672c\u76f8\u540c\uff0c\u4f46\u4e5f\u6709\u70b9\u533a\u522b\uff1a\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u4e2a\u5c0f\u6570\u70b9\u7b26\u53f7\u662f\u6709\u6548\u7684\uff0c\u53e6\u5916parseFloat\u4f1a\u5ffd\u7565\u6240\u6709\u524d\u5bfc0\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u4e00\u4e2a\u53ef\u89e3\u6790\u4e3a\u6574\u6570\u7684\u6570\uff0c\u5219\u8fd4\u56de\u6574\u6570\u503c\u800c\u4e0d\u662f\u6d6e\u70b9\u6570\u503c\u3002<\/p>\n<h3>toString(radix)\u65b9\u6cd5<\/h3>\n<p>\u9664undefined\u548cnull\u4e4b\u5916\u7684\u6240\u6709\u7c7b\u578b\u7684\u503c\u90fd\u5177\u6709toString()\u65b9\u6cd5\uff0c\u5176\u4f5c\u7528\u662f\u8fd4\u56de\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u5bf9\u8c61<\/th>\n<th style=\"text-align: center;\">\u64cd\u4f5c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">Array<\/td>\n<td style=\"text-align: center;\">\u5c06Array\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u7ed3\u679c\u5b57\u7b26\u4e32\u7531\u9017\u53f7\u5206\u9694\uff0c\u4e14\u8fde\u63a5\u8d77\u6765<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Boolean<\/td>\n<td style=\"text-align: center;\">\u5982\u679cBoolean\u503c\u662ftrue\uff0c\u5219\u8fd4\u56de&quot;true&quot;\u3002\u5426\u5219\uff0c\u8fd4\u56de&quot;false&quot;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Date<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u65e5\u671f\u7684\u6587\u5b57\u8868\u793a\u6cd5<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Error<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u76f8\u5173\u9519\u8bef\u4fe1\u606f\u7684\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Function<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u5982\u4e0b\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u4e2dfunctionname\u662f\u88ab\u8c03\u7528toString\u65b9\u6cd5\u51fd\u6570\u7684\u540d\u79f0\uff1a<br \/>function functionname( ) { [native code] }<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Number<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u6570\u5b57\u7684\u6587\u5b57\u8868\u793a<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">String<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56deString\u5bf9\u8c61\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u9ed8\u8ba4<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de&quot;[object objectname]&quot;\uff0c\u5176\u4e2dobjectname\u662f\u5bf9\u8c61\u7c7b\u578b\u7684\u540d\u79f0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>String(mix)\u51fd\u6570<\/h3>\n<p>\u5c06\u4efb\u4f55\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5176\u89c4\u5219\u4e3a\uff1a<\/p>\n<p>\uff081\uff09\u5982\u679c\u6709toString()\u65b9\u6cd5\uff0c\u5219\u8c03\u7528\u8be5\u65b9\u6cd5\uff08\u4e0d\u4f20\u9012radix\u53c2\u6570\uff09\u5e76\u8fd4\u56de\u7ed3\u679c<br \/>\n\uff082\uff09\u5982\u679c\u662fnull\uff0c\u8fd4\u56de&quot;null&quot;<br \/>\n\uff083\uff09\u5982\u679c\u662fundefined\uff0c\u8fd4\u56de&quot;undefined&quot;<\/p>\n<h3>Boolean(mix)\u51fd\u6570<\/h3>\n<p>\u5c06\u4efb\u4f55\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002<\/p>\n<p>\u4ee5\u4e0b\u503c\u4f1a\u88ab\u8f6c\u6362\u4e3afalse\uff1a<code>false<\/code>\u3001<code>&quot;&quot;<\/code>\u3001<code>0<\/code>\u3001<code>NaN<\/code>\u3001<code>null<\/code>\u3001<code>undefined<\/code>\uff0c\u5176\u4f59\u4efb\u4f55\u503c\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3atrue\u3002<\/p>\n<h2>\u9690\u5f0f\u8f6c\u6362<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5373\u4f7f\u6211\u4eec\u4e0d\u63d0\u4f9b\u663e\u793a\u8f6c\u6362\uff0cJavascript\u4e5f\u4f1a\u8fdb\u884c\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\uff0c\u4e3b\u8981\u60c5\u51b5\u6709\uff1a<\/p>\n<h3>\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u4e3a\u975e\u6570\u503c\u7684\u51fd\u6570\uff1aisNaN(mix)<\/h3>\n<p>isNaN()\u51fd\u6570\uff0c\u7ecf\u6d4b\u8bd5\u53d1\u73b0\uff0c\u8be5\u51fd\u6570\u4f1a\u5c1d\u8bd5\u5c06\u53c2\u6570\u503c\u7528Number()\u8fdb\u884c\u8f6c\u6362\uff0c\u5982\u679c\u7ed3\u679c\u4e3a&quot;\u975e\u6570\u503c&quot;\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002<\/p>\n<h3>\u9012\u589e\u9012\u51cf\u64cd\u4f5c\u7b26\uff08\u5305\u62ec\u524d\u7f6e\u548c\u540e\u7f6e\uff09\u3001\u4e00\u5143\u6b63\u8d1f\u7b26\u53f7\u64cd\u4f5c\u7b26<\/h3>\n<p>\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u9002\u7528\u4e8e\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u7684\u503c\uff0c\u9488\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u503c\uff0c\u8be5\u64cd\u4f5c\u7b26\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff08\u7ecf\u8fc7\u5bf9\u6bd4\u53d1\u73b0\uff0c\u5176\u89c4\u5219\u4e0eNumber()\u89c4\u5219\u57fa\u672c\u76f8\u540c\uff09\uff1a<\/p>\n<p>\uff081\uff09\u5982\u679c\u662f\u5305\u542b\u6709\u6548\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u5b57\u503c\uff08\u8f6c\u6362\u89c4\u5219\u540cNumber()\uff09\uff0c\u5728\u6267\u884c\u52a0\u51cf1\u7684\u64cd\u4f5c\uff0c\u5b57\u7b26\u4e32\u53d8\u91cf\u53d8\u4e3a\u6570\u503c\u53d8\u91cf\u3002<br \/>\n\uff082\uff09\u5982\u679c\u662f\u4e0d\u5305\u542b\u6709\u6548\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u5c06\u53d8\u91cf\u7684\u503c\u8bbe\u7f6e\u4e3aNaN\uff0c\u5b57\u7b26\u4e32\u53d8\u91cf\u53d8\u6210\u6570\u503c\u53d8\u91cf\u3002<br \/>\n\uff083\uff09\u5982\u679c\u662f\u5e03\u5c14\u503cfalse\uff0c\u5148\u5c06\u5176\u8f6c\u6362\u4e3a0\u518d\u6267\u884c\u52a0\u51cf1\u7684\u64cd\u4f5c\uff0c\u5e03\u5c14\u503c\u53d8\u91cf\u7f16\u7a0b\u6570\u503c\u53d8\u91cf\u3002<br \/>\n\uff084\uff09\u5982\u679c\u662f\u5e03\u5c14\u503ctrue\uff0c\u5148\u5c06\u5176\u8f6c\u6362\u4e3a1\u518d\u6267\u884c\u52a0\u51cf1\u7684\u64cd\u4f5c\uff0c\u5e03\u5c14\u503c\u53d8\u91cf\u53d8\u6210\u6570\u503c\u53d8\u91cf\u3002<br \/>\n\uff085\uff09\u5982\u679c\u662f\u6d6e\u70b9\u6570\u503c\uff0c\u6267\u884c\u52a0\u51cf1\u7684\u64cd\u4f5c\u3002<br \/>\n\uff086\uff09\u5982\u679c\u662f\u5bf9\u8c61\uff0c\u5148\u8c03\u7528\u5bf9\u8c61\u7684valueOf()\u65b9\u6cd5\uff0c\u7136\u540e\u5bf9\u8be5\u8fd4\u56de\u503c\u5e94\u7528\u524d\u9762\u7684\u89c4\u5219\u3002\u5982\u679c\u7ed3\u679c\u662fNaN\uff0c\u5219\u8c03\u7528toString()\u65b9\u6cd5\u540e\u518d\u5e94\u7528\u524d\u9762\u7684\u89c4\u5219\u3002\u5bf9\u8c61\u53d8\u91cf\u53d8\u6210\u6570\u503c\u53d8\u91cf\u3002<\/p>\n<pre><code class=\"language-javascript\">> a=&quot;2&quot;\n&#039;2&#039;\n> ++a\n3\n> a=&quot;02dd&quot;\n&#039;02dd&#039;\n> ++a\nNaN\n> a=&quot;&quot;\n&#039;&#039;\n> ++a\n1\n> a=false\nfalse\n> ++a\n1\n> a=22.5\n22.5\n> ++a\n23.5\n> a=+&quot;&quot;\n0\n> a=-false\n-0\n> a=+new Date()\n1532335654621\n> a=new Date()\n2018-07-23T08:47:42.504Z\n><\/code><\/pre>\n<h3>\u52a0\u6cd5\u8fd0\u7b97\u64cd\u4f5c\u7b26<\/h3>\n<p>\u52a0\u53f7\u8fd0\u7b97\u64cd\u4f5c\u7b26\u5728JavaScript\u4e5f\u7528\u4e8e\u5b57\u7b26\u4e32\u8fde\u63a5\u7b26\uff0c\u6240\u4ee5\u52a0\u53f7\u64cd\u4f5c\u7b26\u7684\u89c4\u5219\u5206\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<h4>\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u503c\u90fd\u662f\u6570\u503c<\/h4>\n<p>\u5176\u89c4\u5219\u4e3a\uff1a<\/p>\n<p>\uff081\uff09\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u6570\u4e3aNaN\uff0c\u5219\u7ed3\u679c\u4e3aNaN<br \/>\n\uff082\uff09\u5982\u679c\u662fInfinity+Infinity\uff0c\u7ed3\u679c\u662fInfinity<br \/>\n\uff083\uff09\u5982\u679c\u662f-Infinity+(-Infinity)\uff0c\u7ed3\u679c\u662f-Infinity<br \/>\n\uff084\uff09\u5982\u679c\u662fInfinity+(-Infinity)\uff0c\u7ed3\u679c\u662fNaN<br \/>\n\uff085\uff09\u5982\u679c\u662f+0+(+0)\uff0c\u7ed3\u679c\u4e3a+0<br \/>\n\uff086\uff09\u5982\u679c\u662f(-0)+(-0)\uff0c\u7ed3\u679c\u4e3a-0<br \/>\n\uff087\uff09\u5982\u679c\u662f(+0)+(-0)\uff0c\u7ed3\u679c\u4e3a+0<\/p>\n<h4>\u5982\u679c\u6709\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3a\u5b57\u7b26\u4e32<\/h4>\n<p>\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u503c\u90fd\u662f\u5b57\u7b26\u4e32\uff0c\u5219\u5c06\u5b83\u4eec\u62fc\u63a5\u8d77\u6765<br \/>\n\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3a\u5b57\u7b26\u4e32\uff0c\u5219\u5c06\u53e6\u5916\u64cd\u4f5c\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u62fc\u63a5\u8d77\u6765<br \/>\n\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u5bf9\u8c61\u3001\u6570\u503c\u6216\u8005\u5e03\u5c14\u503c\uff0c\u5219\u8c03\u7528toString()\u65b9\u6cd5\u53d6\u5f97\u5b57\u7b26\u4e32\u503c\uff0c\u7136\u540e\u518d\u5e94\u7528\u524d\u9762\u7684\u5b57\u7b26\u4e32\u89c4\u5219\u3002\u5bf9\u4e8eundefined\u548cnull\uff0c\u5206\u522b\u8c03\u7528String()\u663e\u5f0f\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<br \/>\n\u53ef\u4ee5\u770b\u51fa\uff0c\u52a0\u6cd5\u8fd0\u7b97\u4e2d\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5219\u5c06\u53e6\u4e00\u4e2a\u64cd\u4f5c\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<h3>\u4e58\u9664\u3001\u51cf\u53f7\u8fd0\u7b97\u7b26\u3001\u53d6\u6a21\u8fd0\u7b97\u7b26<\/h3>\n<p>\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u9488\u5bf9\u7684\u662f\u8fd0\u7b97\uff0c\u6240\u4ee5\u4ed6\u4eec\u5177\u6709\u5171\u540c\u6027\uff1a\u5982\u679c\u64cd\u4f5c\u503c\u4e4b\u4e00\u4e0d\u662f\u6570\u503c\uff0c\u5219\u88ab\u9690\u5f0f\u8c03\u7528Number()\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002\u5177\u4f53\u6bcf\u4e00\u79cd\u8fd0\u7b97\u7684\u8be6\u7ec6\u89c4\u5219\u8bf7\u53c2\u8003ECMAScript\u4e2d\u7684\u5b9a\u4e49\u3002<\/p>\n<h3>\u903b\u8f91\u64cd\u4f5c\u7b26\uff08!\u3001&amp;&amp;\u3001||\uff09<\/h3>\n<p>\u903b\u8f91\u975e\uff08!\uff09\u64cd\u4f5c\u7b26\u9996\u5148\u901a\u8fc7Boolean()\u51fd\u6570\u5c06\u5b83\u7684\u64cd\u4f5c\u503c\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u7136\u540e\u6c42\u53cd\u3002<\/p>\n<h4>\u903b\u8f91\u4e0e\uff08&amp;&amp;\uff09\u64cd\u4f5c\u7b26<\/h4>\n<p>\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e0d\u662f\u5e03\u5c14\u503c\u65f6\uff0c\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<p>\uff081\uff09\u5982\u679c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u7ecfBoolean()\u8f6c\u6362\u540e\u4e3atrue\uff0c\u5219\u8fd4\u56de\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u503c\uff0c\u5426\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u503c\uff08\u4e0d\u662fBoolean()\u8f6c\u6362\u540e\u7684\u503c\uff09<br \/>\n\uff082\uff09\u5982\u679c\u6709\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3anull\uff0c\u8fd4\u56denull<br \/>\n\uff083\uff09\u5982\u679c\u6709\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3aNaN\uff0c\u8fd4\u56deNaN<br \/>\n\uff084\uff09\u5982\u679c\u6709\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3aundefined\uff0c\u8fd4\u56deundefined<\/p>\n<h4>\u903b\u8f91\u6216\uff08||\uff09\u64cd\u4f5c\u7b26\uff0c\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e0d\u662f\u5e03\u5c14\u503c\uff0c\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a<\/h4>\n<p>\uff081\uff09\u5982\u679c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u503c\u7ecfBoolean()\u8f6c\u6362\u540e\u4e3afalse\uff0c\u5219\u8fd4\u56de\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u503c\uff0c\u5426\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u503c\uff08\u4e0d\u662fBoolean()\u8f6c\u6362\u540e\u7684\u503c\uff09<br \/>\n\uff082\uff09\u5bf9\u4e8eundefined\u3001null\u548cNaN\u7684\u5904\u7406\u89c4\u5219\u4e0e\u903b\u8f91\u4e0e\uff08&amp;&amp;\uff09\u76f8\u540c<\/p>\n<h3>\u5173\u7cfb\u64cd\u4f5c\u7b26\uff08&lt;, &gt;, &lt;=, &gt;=\uff09<\/h3>\n<p>\u4e0e\u4e0a\u8ff0\u64cd\u4f5c\u7b26\u4e00\u6837\uff0c\u5173\u7cfb\u64cd\u4f5c\u7b26\u7684\u64cd\u4f5c\u503c\u4e5f\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u4f7f\u7528\u975e\u6570\u503c\u7c7b\u578b\u53c2\u4e0e\u6bd4\u8f83\u65f6\u4e5f\u9700\u8981\u7cfb\u7edf\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff1a<\/p>\n<p>\uff081\uff09\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u503c\u90fd\u662f\u6570\u503c\uff0c\u5219\u8fdb\u884c\u6570\u503c\u6bd4\u8f83<br \/>\n\uff082\uff09\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u503c\u90fd\u662f\u5b57\u7b26\u4e32\uff0c\u5219\u6bd4\u8f83\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5b57\u7b26\u7f16\u7801\u503c<br \/>\n\uff083\uff09\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u64cd\u4f5c\u503c\u662f\u6570\u503c\uff0c\u5219\u5c06\u53e6\u4e00\u4e2a\u64cd\u4f5c\u503c\u8f6c\u6362\u4e3a\u6570\u503c\uff0c\u8fdb\u884c\u6570\u503c\u6bd4\u8f83<br \/>\n\uff084\uff09\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u5bf9\u8c61\uff0c\u5219\u8c03\u7528valueOf()\u65b9\u6cd5\uff08\u5982\u679c\u5bf9\u8c61\u6ca1\u6709valueOf()\u65b9\u6cd5\u5219\u8c03\u7528toString()\u65b9\u6cd5\uff09\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u6309\u7167\u524d\u9762\u7684\u89c4\u5219\u6267\u884c\u6bd4\u8f83<br \/>\n\uff085\uff09\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u503c\u662f\u5e03\u5c14\u503c\uff0c\u5219\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u503c\uff0c\u518d\u8fdb\u884c\u6bd4\u8f83<\/p>\n<p>\u6ce8\uff1aNaN\u662f\u975e\u5e38\u7279\u6b8a\u7684\u503c\uff0c\u5b83\u4e0d\u548c\u4efb\u4f55\u7c7b\u578b\u7684\u503c\u76f8\u7b49\uff0c\u5305\u62ec\u5b83\u81ea\u5df1\uff0c\u540c\u65f6\u5b83\u4e0e\u4efb\u4f55\u7c7b\u578b\u7684\u503c\u6bd4\u8f83\u5927\u5c0f\u65f6\u90fd\u8fd4\u56defalse<\/p>\n<h3>\u76f8\u7b49\u64cd\u4f5c\u7b26\uff08==\uff09<\/h3>\n<p>\u76f8\u7b49\u64cd\u4f5c\u7b26\u4f1a\u5bf9\u64cd\u4f5c\u503c\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u540e\u8fdb\u884c\u6bd4\u8f83\uff1a<\/p>\n<p>\uff081\uff09\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3a\u5e03\u5c14\u503c\uff0c\u5219\u5728\u6bd4\u8f83\u4e4b\u524d\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u503c<br \/>\n\uff082\uff09\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3a\u5b57\u7b26\u4e32\uff0c\u53e6\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3a\u6570\u503c\uff0c\u5219\u901a\u8fc7Number()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c<br \/>\n\uff083\uff09\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u503c\u662f\u5bf9\u8c61\uff0c\u53e6\u4e00\u4e2a\u4e0d\u662f\uff0c\u5219\u8c03\u7528\u5bf9\u8c61\u7684valueOf()\u65b9\u6cd5\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u6309\u7167\u524d\u9762\u7684\u89c4\u5219\u8fdb\u884c\u6bd4\u8f83<br \/>\n\uff084\uff09null\u4e0eundefined\u662f\u76f8\u7b49\u7684<br \/>\n\uff085\uff09\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u503c\u4e3aNaN\uff0c\u5219\u76f8\u7b49\u6bd4\u8f83\u8fd4\u56defalse<br \/>\n\uff086\uff09\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u503c\u90fd\u662f\u5bf9\u8c61\uff0c\u5219\u6bd4\u8f83\u5b83\u4eec\u662f\u4e0d\u662f\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u663e\u5f0f\u8f6c\u6362 \u901a\u8fc7\u624b\u52a8\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0cJavascript\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u8f6c\u578b\u51fd\u6570\uff1a \u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\uff1aNumber(mix [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[84],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-frontend-basic","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}