{"id":32410,"date":"2024-11-25T15:01:58","date_gmt":"2024-11-25T07:01:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32410\/"},"modified":"2024-11-25T15:01:58","modified_gmt":"2024-11-25T07:01:58","slug":"%e5%88%a4%e6%96%ad%e6%98%af%e5%90%a6%e4%b8%ba%e5%b8%83%e5%b0%94%e5%80%bc%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32410\/","title":{"rendered":"\u5224\u65ad\u662f\u5426\u4e3a\u5e03\u5c14\u503c\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169344871324010.jpg\" class=\"aligncenter\" title=\"\u5224\u65ad\u662f\u5426\u4e3a\u5e03\u5c14\u503c\u7684\u65b9\u6cd5\u63d2\u56fe\" alt=\"\u5224\u65ad\u662f\u5426\u4e3a\u5e03\u5c14\u503c\u7684\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p>Boolean() \u6784\u9020\u51fd\u6570\u53ef\u7528\u4e8e\u521b\u5efa\u5e03\u5c14\u5bf9\u8c61\u4ee5\u53ca\u5e03\u5c14\u539f\u59cb\u503c\uff0c\u8868\u793a true \u6216 false \u503c\u3002 <\/p>\n<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u8be6\u7ec6\u4ecb\u7ecd\u4e86 JavaScript \u4e2d\u5e03\u5c14\u503c\u7684\u521b\u5efa\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample52.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ Create a Boolean object using the new keyword and the Boolean() constructor.\n\tvar myBoolean1 = new Boolean(false); \/\/ Using new keyword.\n\tconsole.log(typeof myBoolean1); \/\/ Logs 'object'.\n\n\t\/\/ Create a Boolean literal\/primitive by directly using the number constructor without new.\n\tvar myBoolean2 = Boolean(0); \/\/ Without new keyword.\n\tconsole.log(typeof myBoolean2); \/\/ Logs 'boolean'.\n\n\t\/\/ Create Boolean literal\/primitive (constructor leveraged behind the scenes).\n\tvar myBoolean3 = false;\n\tconsole.log(typeof myBoolean3); \/\/ Logs 'boolean'.\n\tconsole.log(myBoolean1, myBoolean2, myBoolean3); \/\/ Logs false false false.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<hr>\n<h2> Boolean() \u53c2\u6570<\/h2>\n<p>Boolean() \u6784\u9020\u51fd\u6570\u5c06\u4e00\u4e2a\u53c2\u6570\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff08\u5373 true \u6216 false\uff09\u3002\u4efb\u4f55\u975e 0\u3001-0\u3001null\u3001false\u3001NaN\u3001undefined \u6216\u7a7a\u5b57\u7b26\u4e32 (&#8220;&#8221;) \u7684\u6709\u6548 JavaScript \u503c\u90fd\u5c06\u8f6c\u6362\u4e3a true\u3002\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e24\u4e2a\u5e03\u5c14\u5bf9\u8c61\u503c\uff1a\u4e00\u4e2a true \u548c\u4e00\u4e2a false\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample53.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ Parameter passed to Boolean() = 0 = false, thus foo = false\n\tvar foo = new Boolean(0)\n\tconsole.log(foo);\n\n\t\/\/ Parameter passed to Boolean() = Math = true, thus bar = true\n\tvar bar = new Boolean(Math)\n\tconsole.log(bar);\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5f53\u4e0e new \u5173\u952e\u5b57\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u6765\u81ea Boolean() \u6784\u9020\u51fd\u6570\u7684\u5b9e\u4f8b\u4f1a\u751f\u6210\u4e00\u4e2a\u5b9e\u9645\u7684\u590d\u6742\u5bf9\u8c61\u3002\u60a8\u5e94\u8be5\u907f\u514d\u4f7f\u7528 Boolean() \u6784\u9020\u51fd\u6570\u521b\u5efa\u5e03\u5c14\u503c\uff08\u800c\u662f\u4f7f\u7528\u6587\u5b57\/\u539f\u59cb\u6570\u5b57\uff09\uff0c\u56e0\u4e3a\u5b58\u5728\u4e0e typeof \u8fd0\u7b97\u7b26\u76f8\u5173\u7684\u6f5c\u5728\u95ee\u9898\u3002 typeof \u8fd0\u7b97\u7b26\u5c06\u5e03\u5c14\u5bf9\u8c61\u62a5\u544a\u4e3a\u201cobject\u201d\uff0c\u800c\u4e0d\u662f\u60a8\u53ef\u80fd\u671f\u671b\u7684\u539f\u59cb\u6807\u7b7e\uff08\u201cboolean\u201d\uff09\u3002\u6b64\u5916\uff0c\u6587\u5b57\/\u539f\u59cb\u503c\u7684\u5199\u5165\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<hr>\n<h2> Boolean() \u5c5e\u6027\u548c\u65b9\u6cd5<\/h2>\n<p>Boolean() \u5bf9\u8c61\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\uff1a<\/p>\n<p>\u5c5e\u6027\uff08\u4f8b\u5982\uff0cBoolean.prototype;\uff09\uff1a<\/p>\n<ul>\n<li>\u539f\u578b<\/li>\n<\/ul>\n<hr>\n<h2>\u5e03\u5c14\u5bf9\u8c61\u5b9e\u4f8b\u5c5e\u6027\u548c\u65b9\u6cd5<\/h2>\n<p>\u5e03\u5c14\u5bf9\u8c61\u5b9e\u4f8b\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\u548c\u65b9\u6cd5\uff08\u4e0d\u5305\u62ec\u7ee7\u627f\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\uff1a<\/p>\n<p>\u5b9e\u4f8b\u5c5e\u6027\uff08\u4f8b\u5982\uff0cvar myBoolean = false; myBoolean.constructor;\uff09\uff1a<\/p>\n<ul>\n<li>\u6784\u9020\u51fd\u6570<\/li>\n<\/ul>\n<p>\u5b9e\u4f8b\u65b9\u6cd5\uff08\u4f8b\u5982\uff0cvar myNumber = false; myBoolean.toString();\uff09\uff1a<\/p>\n<ul>\n<li>toSource()<\/li>\n<li>toString()<\/li>\n<li>valueOf()<\/li>\n<\/ul>\n<hr>\n<h2>\u975e\u539f\u59cb False \u5e03\u5c14\u5bf9\u8c61\u8f6c\u6362\u4e3a True<\/h2>\n<p>\u4ece Boolean() \u6784\u9020\u51fd\u6570\u521b\u5efa\u7684 false \u5e03\u5c14\u5bf9\u8c61\uff08\u800c\u4e0d\u662f\u539f\u59cb\u503c\uff09\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e14\u5bf9\u8c61\u4f1a\u8f6c\u6362\u4e3a true\u3002\u56e0\u6b64\uff0c\u5f53\u901a\u8fc7 Boolean() \u6784\u9020\u51fd\u6570\u521b\u5efa false \u5e03\u5c14\u5bf9\u8c61\u65f6\uff0c\u8be5\u503c\u672c\u8eab\u4f1a\u8f6c\u6362\u4e3a true\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u6f14\u793a\u4e86 false \u5e03\u5c14\u5bf9\u8c61\u5982\u4f55\u59cb\u7ec8\u662f\u201ctrue\u201d\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample54.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar falseValue = new Boolean(false);\n\n\tconsole.log(falseValue); \/\/ We have a false Boolean object, but objects are truthy.\n\n\tif (falseValue) { \/\/ Boolean objects, even false Boolean objects, are truthy.\n\t\tconsole.log('falseValue is truthy');\n\t}\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u9700\u8981\u5c06\u975e\u5e03\u5c14\u503c\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u53ea\u9700\u4f7f\u7528 Boolean() \u6784\u9020\u51fd\u6570\uff0c\u800c\u4e0d\u4f7f\u7528 new \u5173\u952e\u5b57\uff0c\u8fd4\u56de\u7684\u503c\u5c06\u662f\u539f\u59cb\u503c\u800c\u4e0d\u662f\u5e03\u5c14\u5bf9\u8c61\u3002 p&gt; <\/p>\n<hr>\n<h2>\u67d0\u4e9b\u4e8b\u60c5\u662f\u5047\u7684\uff0c\u5176\u4ed6\u4e00\u5207\u90fd\u662f\u771f\u7684<\/h2>\n<p>\u5df2\u7ecf\u63d0\u5230\u8fc7\uff0c\u4f46\u503c\u5f97\u518d\u6b21\u63d0\u53ca\uff0c\u56e0\u4e3a\u5b83\u4e0e\u8f6c\u6362\u6709\u5173\uff1a\u5982\u679c\u503c\u4e3a 0\u3001-0\u3001null\u3001false\u3001NaN\u3001undefined\uff0c\u6216\u7a7a\u5b57\u7b26\u4e32\uff08 &#8220;&#8221;)\uff0c\u5c31\u662ffalse\u3002\u5982\u679c\u5728\u5e03\u5c14\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\uff0c\u9664\u4e0a\u8ff0\u503c\u4e4b\u5916\u7684 JavaScript \u4e2d\u7684\u4efb\u4f55\u503c\u90fd\u5c06\u8f6c\u6362\u4e3a true\uff08\u5373 if (true) {};\uff09\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample55.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ All of these return a false Boolean value.\n\tconsole.log(Boolean(0));\n\tconsole.log(Boolean(-0));\n\tconsole.log(Boolean(null));\n\tconsole.log(Boolean(false));\n\tconsole.log(Boolean(''));\n\tconsole.log(Boolean(undefined));\n\tconsole.log(Boolean(null));\n\n\t\/\/ All of these return a true Boolean value.\n\tconsole.log(Boolean(1789));\n\tconsole.log(Boolean('false')); \/\/ 'false' as a string is not false the Boolean value.\n\tconsole.log(Boolean(Math));\n\tconsole.log(Boolean(Array()));\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<hr>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u4e86\u89e3\u54ea\u4e9b JavaScript \u503c\u88ab\u7b80\u5316\u4e3a false \u81f3\u5173\u91cd\u8981\uff0c\u8fd9\u6837\u60a8\u5c31\u77e5\u9053\u6240\u6709\u5176\u4ed6\u503c\u90fd\u88ab\u89c6\u4e3a true\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5224\u65ad\u662f\u5426\u4e3a\u5e03\u5c14\u503c\u7684\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Boolean() \u6784\u9020\u51fd\u6570\u53ef\u7528\u4e8e\u521b\u5efa\u5e03\u5c14\u5bf9\u8c61\u4ee5\u53ca\u5e03\u5c14\u539f\u59cb\u503c\uff0c\u8868\u793a true \u6216 false \u503c\u3002 \u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u8be6\u7ec6\u4ecb\u7ecd\u4e86 JavaScript \u4e2d\u5e03\u5c14\u503c\u7684\u521b\u5efa\u3002 \u793a\u4f8b\uff1asample52.html &lt;script&gt; \/\/ Create a Boolean object using the new keyword and the Boolean() constructor. var myBoolean1 = new Boolean(false); \/\/ Using new keyword. console.log(typeof myBoolean1); \/\/ Logs &#8216;object&#8217;. \/\/ Create a Boolean literal\/primitive by directly using the number constructor without new. var myBoolean2 = Boolean(0); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32410","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32410"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32410\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}