{"id":1460,"date":"2014-03-11T10:49:24","date_gmt":"2014-03-11T02:49:24","guid":{"rendered":"http:\/\/www.gaigaiming.com\/?p=1460"},"modified":"2014-03-11T10:49:24","modified_gmt":"2014-03-11T02:49:24","slug":"js%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/www.gaigaiming.com\/index.php\/2014\/03\/11\/js%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%95%b0%e5%ad%97\/","title":{"rendered":"js\u5b57\u7b26\u4e32\u8f6c\u6570\u5b57"},"content":{"rendered":"<p>\u65b9\u6cd5\u4e3b\u8981\u6709\u4e09\u79cd<br \/>\n\u8f6c\u6362\u51fd\u6570\u3001\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u3001\u5229\u7528js\u53d8\u91cf\u5f31\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>1. \u8f6c\u6362\u51fd\u6570\uff1a<\/p>\n<p>js\u63d0\u4f9b\u4e86parseInt()\u548cparseFloat()\u4e24\u4e2a\u8f6c\u6362\u51fd\u6570\u3002\u524d\u8005\u628a\u503c\u8f6c\u6362\u6210\u6574\u6570\uff0c\u540e\u8005\u628a\u503c\u8f6c\u6362\u6210\u6d6e\u70b9\u6570\u3002\u53ea\u6709\u5bf9String\u7c7b\u578b\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u624d\u80fd\u6b63\u786e\u8fd0\u884c\uff1b\u5bf9\u5176\u4ed6\u7c7b\u578b\u8fd4\u56de\u7684\u90fd\u662fNaN(Not a Number)\u3002<\/p>\n<p>\u4e00\u4e9b\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>parseInt(&#8220;1234blue&#8221;);  \/\/returns  1234<br \/>\nparseInt(&#8220;0xA&#8221;);  \/\/returns  10<br \/>\nparseInt(&#8220;22.5&#8221;);  \/\/returns  22<br \/>\nparseInt(&#8220;blue&#8221;);  \/\/returns  NaN<\/p>\n<p>parseInt()\u65b9\u6cd5\u8fd8\u6709\u57fa\u6a21\u5f0f\uff0c\u53ef\u4ee5\u628a\u4e8c\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u6216\u5176\u4ed6\u4efb\u4f55\u8fdb\u5236\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\u3002\u57fa\u662f\u7531parseInt()\u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u7684\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>parseInt(&#8220;AF&#8221;,  16);  \/\/returns  175<br \/>\nparseInt(&#8220;10&#8221;,  2);  \/\/returns  2<br \/>\nparseInt(&#8220;10&#8221;,  8);  \/\/returns  8<br \/>\nparseInt(&#8220;10&#8221;,  10);  \/\/returns  10<br \/>\n\u5982\u679c\u5341\u8fdb\u5236\u6570\u5305\u542b\u524d\u5bfc0\uff0c\u90a3\u4e48\u6700\u597d\u91c7\u7528\u57fa\u657010\uff0c\u8fd9\u6837\u624d\u4e0d\u4f1a\u610f\u5916\u5730\u5f97\u5230\u516b\u8fdb\u5236\u7684\u503c\u3002\u4f8b\u5982\uff1a<br \/>\nparseInt(&#8220;010&#8221;);  \/\/returns  8<br \/>\nparseInt(&#8220;010&#8221;,  8);  \/\/returns  8<br \/>\nparseInt(&#8220;010&#8221;,  10);  \/\/returns  10<\/p>\n<p>parseFloat()\u65b9\u6cd5\u4e0eparseInt()\u65b9\u6cd5\u7684\u5904\u7406\u65b9\u5f0f\u76f8\u4f3c\u3002<br \/>\n\u4f7f\u7528parseFloat()\u65b9\u6cd5\u7684\u53e6\u4e00\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u4ee5\u5341\u8fdb\u5236\u5f62\u5f0f\u8868\u793a\u6d6e\u70b9\u6570\uff0cparseFloat()\u6ca1\u6709\u57fa\u6a21\u5f0f\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4f7f\u7528parseFloat()\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<br \/>\nparseFloat(&#8220;1234blue&#8221;);  \/\/returns  1234.0<br \/>\nparseFloat(&#8220;0xA&#8221;);  \/\/returns  NaN<br \/>\nparseFloat(&#8220;22.5&#8221;);  \/\/returns  22.5<br \/>\nparseFloat(&#8220;22.34.5&#8221;);  \/\/returns  22.34<br \/>\nparseFloat(&#8220;0908&#8221;);  \/\/returns  908<br \/>\nparseFloat(&#8220;blue&#8221;);  \/\/returns  NaN<\/p>\n<p>2. \u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/p>\n<p>\u8fd8\u53ef\u4f7f\u7528\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff08type casting\uff09\u5904\u7406\u8f6c\u6362\u503c\u7684\u7c7b\u578b\u3002\u4f7f\u7528\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u8bbf\u95ee\u7279\u5b9a\u7684\u503c\uff0c\u5373\u4f7f\u5b83\u662f\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u3002<br \/>\nECMAScript\u4e2d\u53ef\u7528\u76843\u79cd\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u5982\u4e0b\uff1a<br \/>\nBoolean(value)\u2014\u2014\u628a\u7ed9\u5b9a\u7684\u503c\u8f6c\u6362\u6210Boolean\u578b\uff1b<br \/>\nNumber(value)\u2014\u2014\u628a\u7ed9\u5b9a\u7684\u503c\u8f6c\u6362\u6210\u6570\u5b57\uff08\u53ef\u4ee5\u662f\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff09\uff1b<br \/>\nString(value)\u2014\u2014\u628a\u7ed9\u5b9a\u7684\u503c\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u3002<br \/>\n\u7528\u8fd9\u4e09\u4e2a\u51fd\u6570\u4e4b\u4e00\u8f6c\u6362\u503c\uff0c\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u503c\uff0c\u5b58\u653e\u7531\u539f\u59cb\u503c\u76f4\u63a5\u8f6c\u6362\u6210\u7684\u503c\u3002\u8fd9\u4f1a\u9020\u6210\u610f\u60f3\u4e0d\u5230\u7684\u540e\u679c\u3002<br \/>\n\u5f53\u8981\u8f6c\u6362\u7684\u503c\u662f\u81f3\u5c11\u6709\u4e00\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3001\u975e0\u6570\u5b57\u6216\u5bf9\u8c61\uff08\u4e0b\u4e00\u8282\u5c06\u8ba8\u8bba\u8fd9\u4e00\u70b9\uff09\u65f6\uff0cBoolean()\u51fd\u6570\u5c06\u8fd4\u56detrue\u3002\u5982\u679c\u8be5\u503c\u662f\u7a7a\u5b57\u7b26\u4e32\u3001\u6570\u5b570\u3001undefined\u6216null\uff0c\u5b83\u5c06\u8fd4\u56defalse\u3002<\/p>\n<p>\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6bb5\u6d4b\u8bd5Boolean\u578b\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>Boolean(&#8220;&#8221;);  \/\/false  \u2013  empty  string<br \/>\nBoolean(&#8220;hi&#8221;);  \/\/true  \u2013  non-empty  string<br \/>\nBoolean(100);  \/\/true  \u2013  non-zero  number<br \/>\nBoolean(null);  \/\/false  &#8211;  null<br \/>\nBoolean(0);  \/\/false  &#8211;  zero<br \/>\nBoolean(new  Object());  \/\/true  \u2013  object<\/p>\n<p>Number()\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u4e0eparseInt()\u548cparseFloat()\u65b9\u6cd5\u7684\u5904\u7406\u65b9\u5f0f\u76f8\u4f3c\uff0c\u53ea\u662f\u5b83\u8f6c\u6362\u7684\u662f\u6574\u4e2a\u503c\uff0c\u800c\u4e0d\u662f\u90e8\u5206\u503c\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>\u7528\u3000\u3000\u6cd5                  \u7ed3\u3000\u3000\u679c<br \/>\nNumber(false)                  0<br \/>\nNumber(true)                  1<br \/>\nNumber(undefined)              NaN<br \/>\nNumber(null)                  0<br \/>\nNumber( &#8220;5.5 &#8220;)                5.5<br \/>\nNumber( &#8220;56 &#8220;)                56<br \/>\nNumber( &#8220;5.6.7 &#8220;)              NaN<br \/>\nNumber(new  Object())        NaN<br \/>\nNumber(100)                    100 <\/p>\n<p>\u6700\u540e\u4e00\u79cd\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5String()\u662f\u6700\u7b80\u5355\u7684\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>var  s1  =  String(null);  \/\/&#8221;null&#8221;<br \/>\nvar  oNull  =  null;<br \/>\nvar  s2  =  oNull.toString();  \/\/won\u2019t  work,  causes  an  error<\/p>\n<p>3. \u5229\u7528js\u53d8\u91cf\u5f31\u7c7b\u578b\u8f6c\u6362<\/p>\n<p>\u4e3e\u4e2a\u5c0f\u4f8b\u5b50\uff0c\u4e00\u770b\uff0c\u5c31\u4f1a\u660e\u767d\u4e86\u3002<br \/>\n<script> \nvar  str= '012.345 '; \nvar  x  =  str-0; \nx  =  x*1; \n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u4e3b\u8981\u6709\u4e09\u79cd \u8f6c\u6362\u51fd\u6570\u3001\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u3001\u5229\u7528js\u53d8\u91cf\u5f31\u7c7b\u578b\u8f6c\u6362\u3002 1. \u8f6c\u6362\u51fd\u6570\uff1a js\u63d0\u4f9b\u4e86parseInt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1460","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/posts\/1460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/comments?post=1460"}],"version-history":[{"count":0,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/posts\/1460\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/media?parent=1460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/categories?post=1460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/tags?post=1460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}