﻿{"id":930,"date":"2014-06-01T15:25:40","date_gmt":"2014-06-01T07:25:40","guid":{"rendered":"http:\/\/notes.zerobox.org\/?p=930"},"modified":"2014-06-01T15:25:40","modified_gmt":"2014-06-01T07:25:40","slug":"javascript%e4%b8%adnan%e7%9a%84%e7%a7%98%e5%af%86","status":"publish","type":"post","link":"http:\/\/zerobox.org\/notes\/930.html","title":{"rendered":"JavaScript\u4e2dNaN\u7684\u79d8\u5bc6"},"content":{"rendered":"<p>NaN\uff0c\u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u503c\u6765\u4ee3\u8868\u4e0d\u53ef\u8868\u793a\u7684\u503c\uff0c\u4f7f\u7528typeof\u6216\u5176\u4ed6\u4efb\u4f55\u4e0e\u4e4b\u6bd4\u8f83\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u2018NaN\u2019\u5219\u4f1a\u5f15\u8d77\u4e00\u4e9b\u6df7\u4e71\uff0c<\/p>\n<p>\u4e00\u4e9b\u64cd\u4f5c\u4f1a\u5bfc\u81f4NaN\u503c\u7684\u4ea7\u751f\u3002\u8fd9\u91cc\u6709\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<div><\/div>\n<pre>Math.sqrt(-2)\r\nMath.log(-1)\r\n0\/0\r\nparseFloat('foo')<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5bf9\u4e8e\u5f88\u591aJavaScript\u7684\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u5b83\u7684\u7b2c\u4e00\u4e2a\u9677\u9631\u662f\u8c03\u7528typeof\u65f6\u8fd4\u56de\u7ed3\u679c\u7684\u901a\u5e38\u662f\u4f60\u60f3\u4e0d\u5230\u7684\uff1a<\/p>\n<div><\/div>\n<pre>console.log(typeof NaN);\u00a0\u00a0 \/\/ 'number'<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u60c5\u60c5\u51b5\u4e0b\uff0cNaN\u5e76\u4e0d\u610f\u5473\u7740\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u5b83\u7684\u7c7b\u578b\u662f\u6570\u5b57\u3002\u660e\u767d\u5417?<\/p>\n<p>\u4fdd\u6301\u51b7\u9759\uff0c\u56e0\u4e3a\u4e0b\u9762\u8fd8\u6709\u5f88\u591a\u6df7\u4e71\u7684\u5730\u65b9\u3002\u8ba9\u6211\u4eec\u6bd4\u8f83\u4e24\u4e2aNaN\uff1a<\/p>\n<div><\/div>\n<pre>var x = Math.sqrt(-2);\r\nvar y = Math.log(-1);\r\nconsole.log(x == y);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e5f\u8bb8\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u4f7f\u7528\u4e25\u683c\u7b49\u4ef7(===)\u64cd\u4f5c\uff1f\u663e\u7136\u4e0d\u662f\u3002<\/p>\n<div><\/div>\n<pre>var x = Math.sqrt(-2);\r\nvar y = Math.log(-1);\r\nconsole.log(x === y);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u597d\u5427\uff01\u96be\u9053\u662f\u56e0\u4e3a\u8fd9\u4e24\u4e2aNaN\u662f\u4ece\u4e0d\u540c\u7684\u64cd\u4f5c\u4ea7\u751f\u51fa\u6765\u7684\uff1f\u90a3\u4e48\u8fd9\u6837&#8230;<\/p>\n<div><\/div>\n<pre>var x = Math.sqrt(-2);\r\nvar y = Math.sqrt(-2);\r\nconsole.log(x == y);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u518d\u75af\u72c2\u4e00\u70b9<\/p>\n<div><\/div>\n<pre>var x = Math.sqrt(-2);\r\nconsole.log(x == x);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u76f4\u63a5\u6bd4\u8f83\u4e24\u4e2aNaN\u5462\uff1f<\/p>\n<div><\/div>\n<pre>console.log(NaN === NaN); \/\/ false<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u56e0\u4e3a\u6709\u5f88\u591a\u65b9\u6cd5\u6765\u8868\u793a\u4e00\u4e2a\u975e\u6570\u5b57\uff0c\u6240\u4ee5\u4e00\u4e2a\u975e\u6570\u5b57\u4e0d\u4f1a\u7b49\u4e8e\u53e6\u4e00\u4e2a\u4e3aNaN\u7684\u975e\u6570\u5b57\uff0c\u5b83\u8fd8\u662f\u6709\u4e00\u5b9a\u9053\u7406\u7684\u3002\u4e0d\u8fc7\u8fd9\u4e5f\u662f\u6211\u4e3a\u4ec0\u4e48\u65f6\u800c\u5d29\u6e83\u7684\u539f\u56e0\uff1a<\/p>\n<blockquote><p>\u8fd9\u662f\u5bf9\u4f60\u7684\u63d0\u9192\uff0cNaN\u7684\u610f\u601d\u662f\u201c\u4e0d\u4e3aNaN&#8221;.<br \/>\n\u2014 Ariya Hidayat (@AriyaHidayat)<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u672c\u6765\u6211\u6253\u7b97\u5411ECMAScript 7\u63d0\u4ea4\u8be5\u65b9\u6848\u7684<\/p>\n<div><\/div>\n<pre>GarlicNaN != NaN<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f46\u662f\u5f53\u7136\uff0c\u89e3\u51b3\u65b9\u6848\u73b0\u5728\u5df2\u7ecf\u6709\u4e86\u3002<br \/>\n\u8ba9\u6211\u4eec\u8ba4\u8bc6\u4e00\u4e0b\u5168\u5c40\u51fd\u6570isNaN\uff1a<\/p>\n<div><\/div>\n<pre>console.log(isNaN(NaN));\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ true<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5509\uff0c\u4e0d\u8fc7 isNaN() \u4e5f\u6709\u5b83\u81ea\u5df1\u7684\u5f88\u591a\u7f3a\u9677\u5440\uff1a<\/p>\n<div><\/div>\n<pre>console.log(isNaN('hello'));\u00a0 \/\/ true\r\nconsole.log(isNaN(['x']));\u00a0\u00a0\u00a0 \/\/ true\r\nconsole.log(isNaN({}));\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ true<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u6837\u53c8\u4ea7\u751f \u4e86\u5f88\u591a\u4e0d\u540c\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5176\u4e2d\u4e00\u4e2a\u662f\u5229\u7528 \u4e86NaN\u7684\u975e\u53cd\u5c04\u6027\u8d28\uff08\u4f8b\u5982\uff0c \u770b\u770b Kit Cambridge \u7684\u7b14\u8bb0\uff09<\/p>\n<div><\/div>\n<pre>var My = {\r\n\u00a0 isNaN: function (x) { return x !== x; }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u662f\u5148\u68c0\u67e5\u503c\u7684\u7c7b\u578b\uff08\u9632\u6b62\u5f3a\u5236\u8f6c\u6362\uff09\uff1a<\/p>\n<div><\/div>\n<pre>My.isNaN = function(x) { return typeof x === 'number' &amp;&amp; isNaN(x)};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e0d\u8fc7\u5e78\u8fd0\u7684\u662f\uff0c\u5728\u5373\u5c06\u5230\u6765\u7684ECMAScript 6\u4e2d, \u6709\u4e00\u4e2aNumber.isNaN() \u65b9\u6cd5\u63d0\u4f9b\u53ef\u9760\u7684NaN\u503c\u68c0\u6d4b\u3002\uff08\u968f\u4fbf\u8bf4\u4e0b\uff0c\u4f60\u5df2\u7ecf\u53ef\u4ee5\u5728\u6700\u65b0\u7248\u7684Chrome\u548cfirefox\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u4e86\uff09\u3002\u57282014\u5e744\u6708\u7684\u89c4\u8303\u8349\u7a3f\u4e2d\uff0c\u6709\u7740\u5982\u4e0b\u8bb0\u8f7d\uff1a<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p>\u5f53\u4f20\u5165\u4e00\u4e2a\u6570\u5b57\u53c2\u6570\u5e76\u8c03\u7528 Number.isNaN \u65f6\uff0c\u4f1a\u8fdb\u884c\u4ee5\u4e0b\u51e0\u6b65\uff1a<\/p>\n<p>1. \u5982\u679cType(number) \u4e0d\u662f\u6570\u5b57, \u8fd4\u56de false.<br \/>\n2. \u5982\u679c\u6570\u5b57\u662fNaN, \u8fd4\u56detrue.<br \/>\n3. \u5176\u4ed6\u60c5\u51b5\uff0c\u8fd4\u56defalse.<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u53ea\u6709\u5728\u53c2\u6570\u662f\u771f\u6b63\u7684NaN\u65f6\uff0c\u624d\u4f1a\u8fd4\u56detrue<\/p>\n<pre>console.log(Number.isNaN(NaN));            \/\/ true\r\nconsole.log(Number.isNaN(Math.sqrt(-2)));  \/\/ true\r\nconsole.log(Number.isNaN('hello'));\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false\r\nconsole.log(Number.isNaN(['x']));\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false\r\nconsole.log(Number.isNaN({}));\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e0b\u4e00\u6b21\uff0c\u5f53\u4f60\u9700\u8981\u5904\u7406NaN\u65f6\uff0c\u8981\u683c\u5916\u5c0f\u5fc3\u4e86\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u9644\uff1a Poetro \u7684\u8bc4\u8bba<\/p>\n<p>\u8fd9\u662f IEEE 754 \u5b9a\u4e49\u7684NaN\u6807\u51c6, \u8ddfJavaScript\u65e0\u5173\u3002<\/p>\n<div><\/div>\n<blockquote><p>&#8220;\u5728\u8ba1\u7b97\u4e2d, NaN, \u4ee3\u8868\u4e00\u4e2a\u975e\u6570\u5b57, \u662f\u7528\u6765\u5448\u73b0\u672a\u5b9a\u4e49\uff08undefined)\u548c\u4e0d\u53ef\u5448\u73b0\uff08unrepresentable\uff09\u7684\u6570\u636e\u7c7b\u578b, \u5c24\u5176\u662f\u5728\u6d6e\u70b9\u8ba1\u7b97\u4e2d\u3002&#8221;<br \/>\n&#8220;\u4e0eNaN\u7684\u6bd4\u8f83\u4f1a\u4e00\u76f4\u8fd4\u56de\u4e00\u4e2a\u65e0\u5e8f\u7684\u7ed3\u679c\uff0c\u751a\u81f3\u8ddf\u5b83\u81ea\u5df1\u6bd4\u8f83\u3002&#8221;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>NaN\uff0c\u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u503c\u6765\u4ee3\u8868\u4e0d\u53ef\u8868\u793a\u7684\u503c\uff0c\u4f7f\u7528typeof\u6216\u5176\u4ed6\u4efb &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/zerobox.org\/notes\/930.html\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[117],"class_list":["post-930","post","type-post","status-publish","format-standard","hentry","tag-javascript"],"views":442,"_links":{"self":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/comments?post=930"}],"version-history":[{"count":0,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/930\/revisions"}],"wp:attachment":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/media?parent=930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/categories?post=930"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/tags?post=930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}