{"id":1703,"date":"2011-06-21T13:34:22","date_gmt":"2011-06-21T05:34:22","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1703"},"modified":"2018-01-02T00:05:14","modified_gmt":"2018-01-01T16:05:14","slug":"javascript%e6%9c%89%e5%85%b3%e7%9a%8410%e4%b8%aa%e6%80%aa%e7%99%96%e5%92%8c%e7%a7%98%e5%af%86","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2011\/06\/javascript%e6%9c%89%e5%85%b3%e7%9a%8410%e4%b8%aa%e6%80%aa%e7%99%96%e5%92%8c%e7%a7%98%e5%af%86\/","title":{"rendered":"\u7ffb\u7f16-JavaScript\u6709\u5173\u768410\u4e2a\u602a\u7656\u548c\u79d8\u5bc6"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a><br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1703\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=1703<\/a><\/p>\n<p>\u6807\u9898\u4e2d\u7684\u201c\u7ffb\u7f16\u201d\u5b9e\u9645\u4e0a\u662f\u201c\u7ffb\u8bd1\u7f16\u8f91\u201d\u7684\u7b80\u79f0\uff0c\u672c\u6587\u5185\u5bb9\u5e76\u4e0d\u662f\u9010\u8bcd\u9010\u53e5\u7684\u7ffb\u8bd1\uff0c\u6838\u5fc3\u610f\u601d\uff0c\u5927\u4f53\u5185\u5bb9\u4e00\u81f4\uff0c\u53e6\u5916\u81ea\u5df1\u6dfb\u52a0\u4e86\u5f88\u8be6\u7ec6\u7684demo\u9875\u9762\u548c\u622a\u56fe\uff0c\u4ee5\u53ca\u4fee\u6b63\u4e86\u539f\u6587\u4e2d\u7684\u4e00\u4e9b\u4ee3\u7801\u5c55\u793a\u4e0a\u7684\u9519\u8bef\u3002<\/p>\n<div id=\"link\">\n\u539f\u6587\u4f5c\u8005\uff1a<a href=\"http:\/\/www.mitya.co.uk\/\">Andy Croxall<\/a><br \/>\n\u539f\u6587\u94fe\u63a5\uff1a<a href=\"http:\/\/coding.smashingmagazine.com\/2011\/05\/30\/10-oddities-and-secrets-about-javascript\/\">Ten Oddities And Secrets About JavaScript<\/a><br \/>\n\u7ffb\u8bd1\u7f16\u8f91\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/\">\u5f20\u946b\u65ed<\/a>\n<\/div>\n<p><span class=\"s\">\/\/zxx:\u8fd9\u91cc\u7701\u6389200\u591a\u5b57\u7684\u5520\u53e8\u2026\u2026\u76f4\u63a5\u770b\u7ffb\u8bd1\u7f16\u8f91\u5185\u5bb9<\/span><\/p>\n<h3 style=\"font-size:1.4em; padding-bottom:6px; border-bottom:2px solid #333; margin-bottom:0;\">\u6570\u636e\u7c7b\u578b\u548c\u5b9a\u4e49<\/h3>\n<h3>1. Null\u662f\u4e2a\u5bf9\u8c61<\/h3>\n<p>JavaScript\u4f17\u591a\u7c7b\u578b\u4e2d\u6709\u4e2aNull\u7c7b\u578b\uff0c\u5b83\u6709\u4e2a\u552f\u4e00\u7684\u503cnull, \u5373\u5b83\u7684\u5b57\u9762\u91cf\uff0c\u5b9a\u4e49\u4e3a\u5b8c\u5168\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\u7684\u503c\u3002\u5176\u8868\u73b0\u5f97\u50cf\u4e2a\u5bf9\u8c61\uff0c\u5982\u4e0b\u68c0\u6d4b\u4ee3\u7801\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>alert(typeof null); <span style=\"color:green;\">\/\/\u5f39\u51fa 'object'<\/span><\/pre>\n<\/div>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-null-object.html\">null\u4e3a\u5bf9\u8c61\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_105112.png\" width=\"365\" height=\"290\" \/><br \/>\n\u5c3d\u7ba1<code>typeof<\/code>\u503c\u663e\u793a\u662f<code>\"object\"<\/code>\uff0c\u4f46<code>null<\/code>\u5e76\u4e0d\u8ba4\u4e3a\u662f\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\u3002\u8981\u77e5\u9053\uff0cJavaScript\u4e2d\u7684\u503c\u90fd\u662f\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u6bcf\u4e2a\u6570\u503c\u90fd\u662f<code>Number<\/code>\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u662f<code>Object<\/code>\u5bf9\u8c61\u3002\u56e0\u4e3anull\u662f\u6ca1\u6709\u503c\u7684\uff0c\u6240\u4ee5\uff0c\u5f88\u660e\u663e\uff0cnull\u4e0d\u662f\u4efb\u4f55\u4e1c\u897f\u7684\u5b9e\u4f8b\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u503c\u7b49\u4e8e<code>false<\/code>\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>alert(null instanceof Object); <span style=\"color:green;\">\/\/\u4e3a false<\/span><\/pre>\n<\/div>\n<p>\u8bd1\u8005\u6ce8\uff1anull\u8fd8\u6709\u88ab\u7406\u89e3\u4e3a\u5bf9\u8c61\u5360\u4f4d\u7b26\u4e00\u8bf4<\/p>\n<h3>2. NaN\u662f\u4e2a\u6570\u503c<\/h3>\n<p>NaN\u672c\u610f\u662f\u8868\u793a\u67d0\u4e2a\u503c\u4e0d\u662f\u6570\u503c\uff0c\u4f46\u662f\u5176\u672c\u8eab\u5374\u53c8\u662f\u6570\u503c\uff0c\u4e14\u4e0d\u7b49\u4e8e\u5176\u81ea\u8eab\uff0c\u5f88\u5947\u602a\u5427\uff0c\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>alert(typeof NaN); <span style=\"color:green;\">\/\/\u5f39\u51fa 'Number'<\/span>\nalert(NaN === NaN); <span style=\"color:green;\">\/\/\u4e3a false<\/span><\/pre>\n<\/div>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-nan-is-number.html\">NaN\u662f\u4e2a\u6570\u503c\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_105449.png\" width=\"414\" height=\"304\" \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_105523.png\" width=\"445\" height=\"304\" \/><\/p>\n<p>\u5b9e\u9645\u4e0a<code>NaN<\/code>\u4e0d\u7b49\u4e8e\u4efb\u4f55\u4e1c\u897f\u3002\u8981\u786e\u8ba4\u67d0\u73a9\u610f\u662f\u4e0d\u662f<code>NaN<\/code>\u53ea\u80fd\u4f7f\u7528<code>isNaN<\/code>.<\/p>\n<h3>3. \u65e0\u5173\u952e\u5b57\u7684\u6570\u7ec4\u7b49\u540c\u4e8efalse(\u5173\u4e8eTruthy\u548cFalsy)<\/h3>\n<p>\u4e0b\u9762\u662fJavaScript\u53e6\u4e00\u4e2a\u6781\u54c1\u602a\u7656\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>alert(new Array() == false); <span style=\"color:green;\">\/\/\u4e3a true<\/span><\/pre>\n<\/div>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-no-key-array-false.html\">\u65e0\u5173\u952e\u5b57\u6570\u7ec4\u7b49\u540cflase\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_134408.png\" width=\"414\" height=\"304\" \/><\/p>\n<p>\u60f3\u8981\u77e5\u9053\u8fd9\u91cc\u53d1\u751f\u4e86\u4ec0\u4e48\uff0c\u4f60\u9700\u8981\u7406\u89e3<code>truthy<\/code>\u548c<code>falsy<\/code>\u8fd9\u4e2a\u6982\u5ff5\u3002\u5b83\u4eec\u662f\u4e00\u79cd<code>true\/flase<\/code>\u5b57\u9762\u91cf\u3002\u5728JavaScript\u4e2d\uff0c\u6240\u6709\u7684\u975e<code>Boolean<\/code>\u578b\u503c\u90fd\u4f1a\u5185\u7f6e\u4e00\u4e2aboolean\u6807\u5fd7\uff0c\u5f53\u8fd9\u4e2a\u503c\u88ab\u8981\u6c42\u6709boolean\u884c\u4e3a\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u5185\u7f6e\u5e03\u5c14\u503c\u5c31\u4f1a\u51fa\u73b0\uff0c\u4f8b\u5982\u5f53\u4f60\u8981\u8ddf<code>Boolean<\/code>\u578b\u503c\u6bd4\u5bf9\u7684\u65f6\u5019\u3002<\/p>\n<p>\u56e0\u4e3a\u82f9\u679c\u4e0d\u80fd\u548c\u68a8\u505a\u6bd4\u8f83\uff0c\u6240\u4ee5\u5f53JavaScript\u4e24\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u8981\u6c42\u505a\u6bd4\u8f83\u7684\u65f6\u5019\uff0c\u5b83\u9996\u5148\u4f1a\u5c06\u5176\u5f31\u5316\u6210\u76f8\u540c\u7684\u7c7b\u578b\u3002<code>false<\/code>, <code>undefined<\/code>, <code>null<\/code>, <code>0<\/code>, <code>\"\"<\/code>, <code>NaN<\/code>\u90fd\u5f31\u5316\u6210<code>false<\/code>\u3002\u8fd9\u79cd\u5f3a\u5236\u8f6c\u5316\u5e76\u4e0d\u662f\u4e00\u76f4\u5b58\u5728\u7684\uff0c\u53ea\u6709\u5f53\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u4f7f\u7528\u7684\u65f6\u5019\u3002\u770b\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var someVar = 0;\nalert(someVar == false); <span style=\"color:green;\">\/\/\u663e\u793a true<\/span><\/pre>\n<\/div>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-0-and-false.html\">\u6570\u503c0\u548cfalse\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_143814.png\" width=\"414\" height=\"304\" \/><\/p>\n<p>\u4e0a\u9762\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u8bd5\u56fe\u5c06\u6570\u503c0\u548cboolean\u503cfalse\u505a\u6bd4\u8f83\uff0c\u56e0\u4e24\u8005\u7684\u6570\u636e\u7c7b\u578b\u4e0d\u517c\u5bb9\uff0cJavaScript\u81ea\u52a8\u5f3a\u5236\u8f6c\u6362\u6210\u7edf\u4e00\u7684\u7b49\u540c\u7684<code>truthy<\/code>\u548c<code>falsy<\/code>\uff0c\u5176\u4e2d<code>0<\/code>\u7b49\u540c\u4e8e<code>false<\/code>\uff08\u6b63\u5982\u4e0a\u9762\u6240\u63d0\u53ca\u7684\uff09\u3002<\/p>\n<p>\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u4e86\uff0c\u4e0a\u9762\u4e00\u4e9b\u7b49\u540c<code>false<\/code>\u7684\u503c\u4e2d\u5e76\u6ca1\u6709\u7a7a\u6570\u7ec4\u3002\u53ea\u56e0\u7a7a\u6570\u7ec4\u662f\u4e2a\u602a\u80da\u5b50\uff1a\u5176\u672c\u8eab\u5b9e\u9645\u4e0a\u5c5e\u4e8e<code>truthy<\/code>\uff0c\u4f46\u662f\u5f53\u7a7a\u6570\u7ec4\u4e0e<code>Boolean<\/code>\u578b\u505a\u6bd4\u8f83\u7684\u65f6\u5019\uff0c\u5176\u884c\u4e3a\u8868\u73b0\u53c8\u5c5e\u4e8e<code>falsy<\/code>\u3002\u4e0d\u89e3\uff1f\u8fd9\u662f\u7531\u539f\u56e0\u7684\u3002\u5148\u4e3e\u4e2a\u4f8b\u5b50\u9a8c\u8bc1\u4e0b\u7a7a\u6570\u7ec4\u7684\u5947\u602a\u813e\u6c14\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var someVar = []; <span style=\"color:green;\">\/\/\u7a7a\u6570\u7ec4<\/span>\nalert(someVar == false); <span style=\"color:green;\">\/\/\u7ed3\u679c true<\/span>\nif (someVar) alert('hello'); <span style=\"color:green;\">\/\/alert\u8bed\u53e5\u6267\u884c, \u6240\u4ee5someVar\u5f53\u4f5ctrue<\/span><\/pre>\n<\/div>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-empty-array-true-false.html\">\u7a7a\u6570\u7ec4\u7684true\/false\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff0c\u8fde\u7eed\u5f39\u51fa\u4e24\u4e2a\u6846\u6846\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_155544.png\" width=\"284\" height=\"214\" \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_155614.png\" width=\"284\" height=\"214\" \/><\/p>\n<p>\u8bd1\u8005\u6ce8\uff1a\u4e4b\u6240\u4ee5\u4f1a\u6709\u8fd9\u79cd\u5dee\u5f02\uff0c\u6839\u636e\u4f5c\u8005\u7684\u8bf4\u6cd5\uff0c\u6570\u7ec4\u5185\u7f6etoString()\u65b9\u6cd5\uff0c\u4f8b\u5982\u76f4\u63a5alert\u7684\u65f6\u5019\uff0c\u4f1a\u4ee5join(&#8220;,&#8221;)\u7684\u5f62\u5f0f\u5f39\u51fa\u5b57\u7b26\u4e32\uff0c\u7a7a\u6570\u7ec4\u81ea\u7136\u5c31\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u4e8e\u662f\u7b49\u540cfalse\u3002\u5177\u4f53\u53ef\u53c2\u89c1\u4f5c\u8005\u53e6\u5916\u4e00\u7bc7\u6587\u7ae0\uff0c<a href=\"http:\/\/www.mitya.co.uk\/blog\/2011\/Apr\/Twisted-logic-understanding-truthy-and-falsy-174\">Twisted logic: understanding truthy &amp; falsy<\/a>\u3002\u4e0d\u8fc7\u6211\u4e2a\u4eba\u5947\u602a\u7684\u662f\uff0c\u50cf\u7a7a\u5bf9\u8c61\uff0c\u7a7a\u51fd\u6570\uff0c\u5f31\u7b49\u4e8etrue\u6216\u8005false\u7684\u65f6\u5019\u90fd\u663e\u793afalse\uff0c\u4e3a\u4f55\uff1f\u771f\u7684\u56e0\u4e3a\u6570\u7ec4\u662f\u4e2a\u602a\u80ce\uff0c\u9700\u8981\u7279\u6b8a\u8003\u8651\u5417\uff1f<\/p>\n<p>\u4e3a\u907f\u514d\u5f3a\u5236\u8f6c\u6362\u5728\u6bd4\u8f83\u65b9\u9762\u7684\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5f3a\u7b49\u4e8e(<code>===<\/code>)\u4ee3\u66ff\u5f31\u7b49\u4e8e(<code>==<\/code>)\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>var someVar = 0;\nalert(someVar == false); \/\/\u7ed3\u679c true \u2013 0\u5c5e\u4e8efalsy\nalert(someVar === false); \/\/\u7ed3\u679c false \u2013 zero\u662f\u4e2a\u6570\u503c, \u4e0d\u662f\u5e03\u5c14\u503c<\/pre>\n<\/div>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-==-and-===.html\">\u5f3a\u7b49\u4e8e\u548c\u5f31\u7b49\u4e8e\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\u622a\u56fe(win7 FF4)\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_155544.png\" width=\"284\" height=\"214\" \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_174612.png\" width=\"284\" height=\"214\" \/><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u6df1\u5165\u63a2\u7a76JavaScript\u4e2d\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\u7b49\u4e9b\u7279\u6709\u7684\u7656\u597d\uff0c\u53ef\u4ee5\u53c2\u89c1\u5b98\u65b9\u76f8\u5173\u7684\u6587\u6863\u89c4\u8303\uff1a<a href=\"http:\/\/www.mozilla.org\/js\/language\/E262-3.pdf\">section 11.9.3 of the ECMA-262<\/a><\/p>\n<h3 style=\"font-size:1.4em; padding-bottom:6px; border-bottom:2px solid #333; margin-bottom:0;\">\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<h3>4. replace()\u53ef\u4ee5\u63a5\u53d7\u56de\u8c03\u51fd\u6570<\/h3>\n<p>\u8fd9\u662fJavaScript\u6700\u9c9c\u4e3a\u4eba\u77e5\u7684\u79d8\u5bc6\u4e4b\u4e00\uff0cv1.3\u4e2d\u9996\u6b21\u5f15\u5165\u3002\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c<code>replace()<\/code>\u7684\u4f7f\u7528\u8367\u5149\u7c7b\u4f3c\u4e0b\u9762\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>alert('10 13 21 48 52'.replace(\/\\d+\/g, '*')); <span style=\"color:green;\">\/\/\u7528 * \u66ff\u6362\u6240\u6709\u7684\u6570\u5b57<\/span><\/pre>\n<\/div>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u66ff\u6362\uff0c\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e00\u4e2a\u661f\u53f7\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5728\u66ff\u6362\u53d1\u751f\u7684\u65f6\u5019\u6709\u66f4\u591a\u7684\u63a7\u5236\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f\u6211\u4eec\u53ea\u5e0c\u671b\u66ff\u636230\u4ee5\u4e0b\u7684\u6570\u503c\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f\u6b64\u65f6\u5982\u679c\u4ec5\u4ec5\u4f9d\u9760\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u97ad\u957f\u83ab\u53ca\u7684\u3002\u6211\u4eec\u9700\u8981\u501f\u52a9\u56de\u8c03\u51fd\u6570\u7684\u4e1c\u98ce\u5bf9\u6bcf\u4e2a\u5339\u914d\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>alert('10 13 21 48 52'.replace(\/\\d+\/g, function(match) {\n    return parseInt(match) < 30 ? '*' : match;\n}));<\/pre>\n<\/div>\n<p>\u5f53\u6bcf\u4e2a\u5339\u914d\u5b8c\u6210\u7684\u65f6\u5019\uff0cJavaScript\u5e94\u7528\u56de\u8c03\u51fd\u6570\uff0c\u4f20\u9012\u5339\u914d\u5185\u5bb9\u7ed9<code>match<\/code>\u53c2\u6570\u3002\u7136\u540e\uff0c\u6839\u636e\u56de\u8c03\u51fd\u6570\u91cc\u9762\u7684\u8fc7\u6ee4\u89c4\u5219\uff0c\u8981\u4e48\u8fd4\u56de\u661f\u53f7\uff0c\u8981\u4e48\u8fd4\u56de\u5339\u914d\u672c\u8eab\uff08\u65e0\u66ff\u6362\u53d1\u751f\uff09\u3002<\/p>\n<p>\u5982\u4e0b\u622a\u56fe\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-20_114028.png\" width=\"494\" height=\"224\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-replace-call-function.html\">replace()\u63a5\u53d7\u56de\u8c03\u51fd\u6570demo<\/a><\/p>\n<h3>5. \u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u4e0d\u53ea\u662fmatch\u548creplace<\/h3>\n<p>\u4e0d\u5c11javascript\u5de5\u7a0b\u5e08\u90fd\u662f\u53ea\u901a\u8fc7<code>match<\/code>\u548c<code>replace<\/code>\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u6253\u4ea4\u9053\u3002\u4f46JavaScript\u6240\u5b9a\u4e49\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u76f8\u5173\u65b9\u6cd5\u8fdc\u4e0d\u6b62\u8fd9\u4e24\u4e2a\u3002<\/p>\n<p>\u5176\u4e2d\u503c\u5f97\u4e00\u63d0\u7684\u662f<code>test()<\/code>\uff0c\u5176\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c<code>match()<\/code>\uff0c\u4f46\u662f\u8fd4\u56de\u503c\u5374\u4e0d\u4e00\u6837\uff1a<code>test()<\/code>\u8fd4\u56de\u7684\u662f\u5e03\u5c14\u578b\uff0c\u7528\u6765\u9a8c\u8bc1\u662f\u5426\u5339\u914d\uff0c\u6267\u884c\u901f\u5ea6\u9ad8\u4e8e<code>match()<\/code>\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>alert(\/\\w{3,}\/.test('Hello')); <span style=\"color:green;\">\/\/\u5f39\u51fa 'true'<\/span><\/pre>\n<\/div>\n<p>\u4e0a\u9762\u884c\u4ee3\u7801\u7528\u6765\u9a8c\u8bc1\u5b57\u7b26\u4e32\u662f\u5426\u6709\u4e09\u4e2a\u4ee5\u4e0a\u666e\u901a\u5b57\u7b26\uff0c\u663e\u7136\"hello\"\u662f\u7b26\u5408\u8981\u6c42\u7684\uff0c\u6240\u4ee5\u5f39\u51fa<code>true<\/code>\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-regexp-test.html\">tese()\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-16_155544.png\" width=\"284\" height=\"214\" \/><\/p>\n<p>\u6211\u4eec\u8fd8\u5e94\u6ce8\u610f<code>RegExp<\/code>\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u7528\u6b64\u521b\u5efa\u52a8\u6001\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>function findWord(word, string) {\n    var instancesOfWord = string.match(new RegExp('\\\\b'+word+'\\\\b', 'ig'));\n    alert(instancesOfWord);\n}\nfindWord('car', 'Carl went to buy a car but had forgotten his credit card.');<\/pre>\n<\/div>\n<p>\u8fd9\u513f\uff0c\u6211\u4eec\u57fa\u4e8e\u53c2\u6570<code>word<\/code>\u52a8\u6001\u521b\u5efa\u4e86\u5339\u914d\u9a8c\u8bc1\u3002\u8fd9\u6bb5\u6d4b\u8bd5\u4ee3\u7801\u4f5c\u7528\u662f\u4e0d\u533a\u5206\u5927\u5c0f\u9009\u7684\u60c5\u51b5\u4e0b\u9009\u62e9<code>car<\/code>\u8fd9\u4e2a\u5355\u8bcd\u3002\u773c\u775b\u4e00\u626b\u800c\u8fc7\uff0c\u6d4b\u8bd5\u82f1\u6587\u53e5\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a\u5355\u8bcd\u662f<code>car<\/code>\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684\u6f14\u51fa\u4ec5\u4e00\u4e2a\u5355\u8bcd\u3002<code>\\b<\/code>\u662f\u7528\u6765\u8868\u793a\u5355\u8bcd\u8fb9\u754c\u7684\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-dynamic-regexp.html\">\u4e0a\u8ff0\u52a8\u6001\u6b63\u5219\u8868\u8fbe\u5f0fdemo<\/a><\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-20_230816.png\" width=\"299\" height=\"221\" \/><\/p>\n<h3 style=\"font-size:1.4em; padding-bottom:6px; border-bottom:2px solid #333; margin-bottom:0;\">\u51fd\u6570\u548c\u4f5c\u7528\u57df<\/h3>\n<h3>6. \u4f60\u53ef\u4ee5\u5192\u5145\u4f5c\u7528\u57df<\/h3>\n<p>\u4f5c\u7528\u57df\u8fd9\u73a9\u610f\u662f\u7528\u6765\u51b3\u5b9a\u4ec0\u4e48\u53d8\u91cf\u662f\u53ef\u7528\u7684\uff0c\u72ec\u7acb\u7684JavaScript\uff08\u5982JavaScript\u4e0d\u662f\u8fd0\u884c\u4e2d\u51fd\u6570\u4e2d\uff09\u5728<code>window<\/code>\u5bf9\u8c61\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e0b\u64cd\u4f5c\uff0c<code>window<\/code>\u5bf9\u8c61\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u53ef\u4ee5\u8bbf\u95ee\u3002\u7136\u800c\u51fd\u6570\u4e2d\u58f0\u660e\u7684\u5c40\u90e8\u53d8\u91cf\u53ea\u80fd\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>var animal = 'dog';\nfunction getAnimal(adjective) { alert(adjective+' '+this.animal); }\ngetAnimal('lovely'); <span style=\"color:green;\">\/\/\u5f39\u51fa 'lovely dog'<\/span><\/pre>\n<\/div>\n<p>\u8fd9\u513f\u6211\u4eec\u7684\u53d8\u91cf\u548c\u51fd\u6570\u90fd\u58f0\u660e\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u56e0\u4e3a<code>this<\/code>\u6307\u5411\u5f53\u524d\u4f5c\u7528\u57df\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5c31\u662f<code>window<\/code>\u3002\u56e0\u6b64\uff0c\u8be5\u51fd\u6570\u5bfb\u627e<code>window.animal<\/code>\uff0c\u4e5f\u5c31\u662f<code>'dog'<\/code>\u4e86\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4e00\u5207\u6b63\u5e38\u3002\u7136\u800c\uff0c\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u51fd\u6570\u8fd0\u884c\u5728\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u4e0b\uff0c\u800c\u5ffd\u89c6\u5176\u672c\u8eab\u7684\u4f5c\u7528\u57df\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2a\u5185\u7f6e\u7684\u79f0\u4e3a<code>call()<\/code>\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u4f5c\u7528\u57df\u7684\u5192\u5145\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>var animal = 'dog';\nfunction getAnimal(adjective) { alert(adjective+' '+this.animal); };\nvar myObj = {animal: 'camel'};\ngetAnimal.call(myObj, 'lovely'); <span style=\"color:green;\">\/\/\u5f39\u51fa 'lovely camel'<\/span><\/pre>\n<\/div>\n<p><code>call()<\/code>\u65b9\u6cd5\u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u5192\u5145\u51fd\u6570\u4e2d\u7684<code>this<\/code>\uff0c\u56e0\u6b64\uff0c\u8fd9\u91cc\u7684<code>this.animal<\/code>\u5b9e\u9645\u4e0a\u5c31\u662f<code>myObj.animal<\/code>\uff0c\u4e5f\u5c31\u662f<code>'camel'<\/code>\u4e86\u3002\u540e\u9762\u7684\u53c2\u6570\u5c31\u4f5c\u4e3a\u666e\u901a\u53c2\u6570\u4f20\u7ed9\u51fd\u6570\u4f53\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u4e0e\u4e4b\u76f8\u5173\u7684\u662f<code>apply()<\/code>\u65b9\u6cd5\uff0c\u5176\u4f5c\u7528\u4e8e<code>call()<\/code>\u4e00\u6837\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\u662f\u4ee5\u6570\u7ec4\u5f62\u5f0f\u8868\u793a\u7684\uff0c\u800c\u4e0d\u662f\u72ec\u7acb\u7684\u53d8\u91cf\u4eec\u3002\u6240\u4ee5\uff0c\u4e0a\u9762\u7684\u6d4b\u8bd5\u4ee3\u7801\u5982\u679c\u7528<code>apply()<\/code>\u8868\u793a\u5c31\u662f\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>getAnimal.apply(myObj, ['lovely']); <span style=\"color:green;\">\/\/\u51fd\u6570\u53c2\u6570\u4ee5\u6570\u7ec4\u5f62\u5f0f\u53d1\u9001<\/span><\/pre>\n<\/div>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-function-fake-scope.html\">\u51fd\u6570\u4f5c\u7528\u57df\u5192\u5145demo<\/a><\/p>\n<p>demo\u9875\u9762\u4e2d\uff0c\u70b9\u51fb\u7b2c\u4e00\u4e2a\u6309\u94ae\u7684\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-21_001417.png\" width=\"324\" height=\"224\" \/><br \/>\u70b9\u51fb\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u6309\u94ae\u7684\u7ed3\u679c\u5982\u4e0b\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-21_001445.png\" width=\"315\" height=\"224\" \/><\/p>\n<div class=\"hidden\"><em>\u5982\u679c\u60a8\u770b\u5230\u4e0b\u9762\u7684\u6587\u5b57\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u5728\u5176\u4ed6\u7f51\u7ad9\u6216\u662fRSS\u4e2d\u9605\u8bfb\u672c\u6587\uff0c\u672c\u6587\u539f\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1591\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=1591<\/a>\uff0c\u672c\u6587\u4f5c\u8005\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/\">\u5f20\u946b\u65ed<\/a>\uff0c\u6765\u81ea\u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\uff0c\u8bbf\u95ee\u539f\u51fa\u5904\u66f4\u591a\u7cbe\u5f69\u5185\u5bb9\u3002<\/em><\/div>\n<h3>7. \u51fd\u6570\u53ef\u4ee5\u6267\u884c\u5176\u672c\u8eab<\/h3>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u662f\u5f88OK\u7684\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>(function() { alert('hello'); })(); <span style=\"color:green;\">\/\/\u5f39\u51fa 'hello'<\/span><\/pre>\n<\/div>\n<p>\u8fd9\u91cc\u7684\u89e3\u6790\u8db3\u591f\u7b80\u5355\uff1a\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u56e0\u4e3a<code>()<\/code>\u89e3\u6790\u7acb\u5373\u6267\u884c\u5b83\u3002\u4f60\u53ef\u80fd\u4f1a\u5947\u602a\u4e3a\u4f55\u8981\u8fd9\u4e48\u505a\uff08\u6307\u76f4\u63a5\u5c41\u80a1\u540e\u9762<code>()<\/code>\u8c03\u7528\uff09\uff0c\u8fd9\u770b\u4e0a\u53bb\u662f\u6709\u70b9\u81ea\u76f8\u77db\u76fe\u7684\uff1a\u51fd\u6570\u5305\u542b\u7684\u901a\u5e38\u662f\u6211\u4eec\u60f3\u7a0d\u540e\u6267\u884c\u7684\u4ee3\u7801\uff0c\u800c\u4e0d\u662f\u5f53\u4e0b\u89e3\u6790\u5373\u6267\u884c\u7684\uff0c\u5426\u5219\uff0c\u6211\u4eec\u5c31\u6ca1\u6709\u5fc5\u8981\u628a\u4ee3\u7801\u653e\u5728\u51fd\u6570\u4e2d\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u6267\u884c\u51fd\u6570\u81ea\u8eab(self-executing functions (SEFs))\u7684\u4e0d\u9519\u4f7f\u7528\u662f\u4e3a\u5728\u5ef6\u8fdf\u4ee3\u7801\u4e2d\u4f7f\u7528\u7ed1\u5b9a\u53d8\u91cf\u503c\uff0c\u4f8b\u5982\u4e8b\u4ef6\u7684\u56de\u8c03(callback)\uff0c\u8d85\u65f6\u6267\u884c(timeouts)\u548c\u95f4\u9694\u6267\u884c(intervals)\u3002\u5982\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var someVar = 'hello';\nsetTimeout(function() { alert(someVar); }, 1000);\nvar someVar = 'goodbye';<\/pre>\n<\/div>\n<p>Newbies\u5728\u8bba\u575b\u91cc\u603b\u95ee\u8fd9\u91cc<code>timeout<\/code>\u7684\u5f39\u51fa\u4e3a\u4ec0\u4e48\u662f<code>goodbye<\/code>\u800c\u4e0d\u662f<code>hello<\/code>\uff1f\u7b54\u6848\u5c31<code>timeout<\/code>\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u76f4\u5230\u5176\u8fd0\u884c\u7684\u65f6\u5019\u624d\u53bb\u8d4b\u503c<code>someVar<\/code>\u53d8\u91cf\u7684\u503c\u3002\u800c\u90a3\u4e2a\u65f6\u5019\uff0c<code>someVar<\/code>\u5df2\u7ecf\u88ab<code>goodbye<\/code>\u91cd\u5199\u4e86\u597d\u957f\u65f6\u95f4\u4e86\u3002<\/p>\n<p>SEFs\u63d0\u4f9b\u4e86\u4e00\u4e2a\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u65b9\u6cd5\u3002\u4e0d\u662f\u50cf\u4e0a\u9762\u4e00\u6837\u542b\u84c4\u5730\u6307\u5b9atimeout\u56de\u8c03\uff0c\u800c\u662f\u76f4\u63a5\u5c06<code>someVar<\/code>\u503c\u4ee5\u53c2\u6570\u7684\u5f62\u5f0f\u4f20\u8fdb\u53bb\u3002\u6548\u679c\u663e\u8457\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u4f20\u5165\u5e76\u5b64\u7acb\u4e86<code>someVar<\/code>\u503c\uff0c\u4fdd\u62a4\u5176\u65e0\u8bba\u540e\u9762\u662f\u5730\u9707\u6d77\u5578\u8fd8\u662f\u5973\u670b\u53cb\u53d1\u98d9\u5486\u54ee\u90fd\u4e0d\u4f1a\u6539\u53d8\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>var someVar = 'hello';\nsetTimeout((function(someVar) {\n\treturn function()  { alert(someVar); }\n})(someVar), 1000);\nvar someVar = 'goodbye';<\/pre>\n<\/div>\n<p>\u98ce\u6c34\u8f6e\u6d41\u8f6c\uff0c\u8fd9\u6b21\uff0c\u8fd9\u91cc\u7684\u5f39\u51fa\u5c31\u662f<code>hello<\/code>\u4e86\u3002\u8fd9\u5c31\u662f\u51fd\u6570\u53c2\u6570\u548c\u5916\u90e8\u53d8\u91cf\u7684\u70b9\u5dee\u522b\u4e86\u54c8\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-self-executing-function.html\">\u51fd\u6570\u6267\u884c\u81ea\u8eab\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u4f8b\u5982\uff0c\u6700\u540e\u4e00\u4e2a\u6309\u94ae\u70b9\u51fb\u540e\u7684\u5f39\u51fa\u5982\u4e0b\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-21_103338.png\" width=\"342\" height=\"217\" \/><\/p>\n<h3 style=\"font-size:1.4em; padding-bottom:6px; border-bottom:2px solid #333; margin-bottom:0;\">\u6d4f\u89c8\u5668<\/h3>\n<h3>8. FireFox\u4ee5RGB\u683c\u5f0f\u8bfb\u4e0e\u8fd4\u56de\u989c\u8272\u800c\u975eHex<\/h3>\n<p>\u76f4\u5230\u73b0\u5728\u6211\u90fd\u6ca1\u6709\u771f\u6b63\u7406\u89e3\u4e3a\u4f55Mozilla\u4f1a\u8fd9\u6837\u5b50\u3002\u4e3a\u4e86\u6709\u4e2a\u6e05\u6670\u7684\u8ba4\u8bc6\uff0c\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre><span style=\"color:green;\">&lt;!--\n#somePara { color: #f90; }\n--><\/span>\n\n&lt;p id=\"somePara\">Hello, world!&lt;\/p>\n\n<span style=\"color:#069;\">&lt;script><\/span>\nvar ie = navigator.appVersion.indexOf('MSIE') != -1;\nvar p = document.getElementById('somePara');\nalert(ie ? p.currentStyle.color : getComputedStyle(p, null).color);\n<span style=\"color:#069;\">&lt;\/script><\/span><\/pre>\n<\/div>\n<p>\u5927\u90e8\u5206\u6d4f\u89c8\u5668\u5f39\u51fa\u7684\u7ed3\u679c\u662f<code>ff9900<\/code>\uff0c\u800cFireFox\u7684\u7ed3\u679c\u5374\u662f<code>rgb(255, 153, 0)<\/code>\uff0cRGB\u7684\u5f62\u5f0f\u3002\u7ecf\u5e38\uff0c\u5904\u7406\u989c\u8272\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u82b1\u8d39\u4e0d\u5c11\u4ee3\u7801\u5c06RGB\u989c\u8272\u8f6c\u4e3aHex\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e0a\u9762\u4ee3\u7801\u5728\u4e0d\u540c\u6d4f\u89c8\u5668\u4e0b\u7684\u7ed3\u679c\uff1a<br \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-21_112753.png\" width=\"338\" height=\"234\" \/><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-21_112845.png\" width=\"319\" height=\"234\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201106\/js-odd-secret-color-rgb-hex.html\">\u4e0d\u540c\u6d4f\u89c8\u5668\u7684\u989c\u8272\u8fd4\u56de\u5dee\u5f02demo<\/a><\/p>\n<h3 style=\"font-size:1.4em; padding-bottom:6px; border-bottom:2px solid #333; margin-bottom:0;\">\u5176\u5b83\u6742\u4e03\u6742\u516b<\/h3>\n<h3>9. 0.1 + 0.2 !== 0.3<\/h3>\n<p>\u8fd9\u4e2a\u53e4\u602a\u7684\u95ee\u9898\u4e0d\u53ea\u4f1a\u51fa\u73b0\u5728JavaScript\u4e2d\uff0c\u8fd9\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u4e00\u4e2a\u666e\u904d\u5b58\u5728\u7684\u95ee\u9898\uff0c\u5f71\u54cd\u4e86\u5f88\u591a\u7684\u8bed\u8a00\u3002\u6807\u9898\u7b49\u5f0f\u8f93\u51fa\u7684\u7ed3\u679c\u662f0.30000000000000004\u3002<\/p>\n<p>\u8fd9\u662f\u4e2a\u88ab\u79f0\u4e3a\u673a\u5668\u7cbe\u5ea6\u7684\u95ee\u9898\u3002\u5f53JavaScript\u5c1d\u8bd5\u6267\u884c(0.1 + 0.2)\u8fd9\u884c\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4f1a\u628a\u503c\u8f6c\u6362\u6210\u5b83\u4eec\u559c\u6b22\u7684\u4e8c\u8fdb\u5236\u53e3\u5473\u3002\u8fd9\u5c31\u662f\u95ee\u9898\u7684\u8d77\u6e90\uff0c0.1\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f0.1\uff0c\u800c\u662f\u5176\u4e8c\u8fdb\u5236\u5f62\u5f0f\u3002\u4ece\u672c\u8d28\u4e0a\u5c06\uff0c\u5f53\u4f60\u5199\u4e0b\u8fd9\u4e9b\u503c\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u6ce8\u5b9a\u8981\u5931\u53bb\u7cbe\u5ea6\u3002\u4f60\u53ef\u80fd\u53ea\u662f\u5e0c\u671b\u5f97\u5230\u4e2a\u7b80\u5355\u7684\u4e24\u4f4d\u5c0f\u6570\uff0c\u4f46\u4f60\u5f97\u5230\u7684\uff08\u6839\u636eChris Pine\u7684<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/dev.opera.com\/articles\/view\/why-i-love-ecmascript-4-real-decimals\/\">\u6ce8\u89e3<\/a>\uff09\u662f\u4e8c\u8fdb\u5236\u6d6e\u70b9\u8ba1\u7b97\u3002\u597d\u6bd4\u4f60\u60f3\u628a\u4e00\u6bb5\u5e94\u8be5\u7ffb\u8bd1\u6210\u4e2d\u6587\u7b80\u4f53\uff0c\u7ed3\u679c\u51fa\u6765\u7684\u786e\u5b9e\u7e41\u4f53\uff0c\u5176\u4e2d\u8fd8\u662f\u6709\u5dee\u5f02\u662f\u4e0d\u4e00\u6837\u7684\u3002<\/p>\n<p>\u4e00\u822c\u5904\u7406\u4e0e\u6b64\u76f8\u5173\u7684\u95ee\u9898\u6709\u4e24\u4e2a\u505a\u6cd5\uff1a<\/p>\n<ol>\n<li>\u8f6c\u6362\u6210\u6574\u6570\u5728\u8ba1\u7b97\uff0c\u8ba1\u7b97\u5b8c\u6bd5\u5728\u8f6c\u6362\u6210\u5e0c\u671b\u7684\u5c0f\u6570\u5185\u5bb9<\/li>\n<li>\u8c03\u6574\u4f60\u7684\u903b\u8f91\uff0c\u8bbe\u5b9a\u5141\u8bb8\u8303\u56f4\u4e3a\u4e0d\u662f\u6307\u5b9a\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u4e0d\u5e94\u8be5\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var num1 = 0.1, num2 = 0.2, shouldEqual = 0.3;\nalert(num1 + num2 == shouldEqual); <span style=\"color:green;\">\/\/false<\/span><\/pre>\n<\/div>\n<p>\u800c\u53ef\u4ee5\u8bd5\u8bd5\u8fd9\u6837\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>alert(num1 + num2 > shouldEqual - 0.001 && num1 + num2 < shouldEqual + 0.001); <span style=\"color:green;\">\/\/true<\/span><\/pre>\n<\/div>\n<h3>10. \u672a\u5b9a\u4e49(undefined)\u53ef\u4ee5\u88ab\u5b9a\u4e49(defined)<\/h3>\n<p>\u6211\u4eec\u4ee5\u4e00\u4e2a\u548c\u98ce\u7ec6\u96e8\u7684\u5c0f\u53e4\u602a\u7ed3\u675f\u3002\u542c\u8d77\u6765\u53ef\u80fd\u6709\u70b9\u5947\u602a\uff0c<code>undefined<\/code>\u5e76\u4e0d\u662fJavaScript\u4e2d\u7684\u4fdd\u7559\u5b57\uff0c\u5c3d\u7ba1\u5b83\u6709\u7279\u6b8a\u7684\u610f\u4e49\uff0c\u5e76\u4e14\u662f\u552f\u4e00\u7684\u65b9\u6cd5\u786e\u5b9e\u53d8\u91cf\u662f\u5426\u672a\u5b9a\u4e49\u3002\u56e0\u6b64\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var someVar;\nalert(someVar == undefined); <span style=\"color:green;\">\/\/\u663e\u793a true<\/span><\/pre>\n<\/div>\n<p>\u76ee\u524d\u4e3a\u6b62\uff0c\u4e00\u5207\u770b\u4e0a\u53bb\u98ce\u5e73\u6d6a\u9759\uff0c\u6b63\u5e38\u65e0\u6bd4\uff0c\u4f46\u5267\u60c5\u603b\u662f\u5f88\u72d7\u8840\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>undefined = \"I'm not undefined!\";\nvar someVar;\nalert(someVar == undefined); <span style=\"color:green;\">\/\/\u663e\u793a false!<\/span><\/pre>\n<\/div>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48jQuery\u6e90\u7801\u4e2d\u6700\u5916\u90e8\u7684\u95ed\u5305\u51fd\u6570\u8981\u6709\u4e2a\u5e76\u6ca1\u6709\u4f20\u5165\u7684<code>undefined<\/code>\u53c2\u6570\uff0c\u76ee\u7684\u5c31\u662f\u4fdd\u62a4<code>undefined<\/code>\u4e0d\u8981\u88ab\u5916\u90e8\u7684\u4e9b\u4e0d\u826f\u4e58\u865a\u800c\u5165\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/2011-06-21_121422.png\" width=\"438\" height=\"223\" \/><\/p>\n<p><spam class=\"s\">\/\/zxx:\u4ee5\u4e0a\u4e3a\u4e3b\u4f53\u5168\u90e8\u5185\u5bb9<\/span><\/p>\n<h3>\u8bd1\u8005\u7ed3\u8bed<\/h3>\n<p>\u6700\u8fd1\u7410\u4e8b\u592a\u591a\uff0c\u6587\u7ae0\u6298\u817e\u7684\u6709\u70b9\u4e45\u3002\u6700\u8fd1\u72b6\u6001\u4e0d\u4f73\uff0c\u6587\u4e2d\u96be\u514d\u6709\u8868\u8ff0\u4e0d\u51c6\u786e\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u6307\u6b63\uff0c\u611f\u8c22\u9605\u8bfb\u3002<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u81ea<a href=\"http:\/\/www.zhangxinxu.com\/\">\u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<\/a>[<a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a>]<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1703\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=1703<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1703\"><img loading=\"lazy\" decoding=\"async\" class=\"imgpad mb10\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201106\/js-odd-secret.jpg\" width=\"550\" height=\"170\" \/><\/a><br \/>\n\u672c\u8eab\u5c5e\u4e8e\u7ffb\u8bd1\u6587\u7ae0\uff0c\u5185\u5bb9\u4e3aJavaScript\u7684\u4e00\u4e9b\u5947\u602a\u4e4b\u5904\uff0c\u4f46\u662f\u53c8\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u9010\u5b57\u9010\u53e5\u5730\u7ffb\u8bd1\uff0c\u5927\u4f53\u610f\u601d\u5230\u4e86\uff0c\u81f3\u4e8e\u7ec6\u8282\u4ee5\u53ca\u6587\u5b57\u8868\u8ff0\u8fd8\u662f\u6709\u4e9b\u672c\u571f\u5316\u7684\u7279\u5f81\uff0c\u5e76\u5728\u6709\u4e9b\u5730\u65b9\u5916\u52a0\u4e86\u70b9\u81ea\u5df1\u7684\u5c0f\u770b\u6cd5\u3002\u540c\u65f6\u4e3a\u4e86\u4fbf\u4e8e\u5927\u5bb6\u7684\u7406\u89e3\u4e0e\u5b66\u4e60\uff0c\u6211\u8fd8\u5bf9\u539f\u6587\u5185\u5bb9\u8fdb\u884c\u4e86\u6269\u5145\uff0c\u5373\u57fa\u672c\u4e0a\u9488\u5bf9\u6bcf\u4e00\u4e2a\u5b9e\u4f8b\u90fd\u5236\u4f5c\u4e86\u76f8\u5e94\u7684demo\u9875\u9762\u5e76\u9644\u4e0a\u4e86\u76f8\u5173\u7684\u622a\u56fe\u3002\u5e0c\u671b\u80fd\u5bf9\u60a8\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,198],"tags":[13,61,504,505,211,184,509,222],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/1703"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1703"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/1703\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}