{"id":8037,"date":"2024-06-25T16:01:01","date_gmt":"2024-06-25T08:01:01","guid":{"rendered":""},"modified":"2024-06-25T16:01:01","modified_gmt":"2024-06-25T08:01:01","slug":"\u52a0\u53f7\u7684\u516c\u5f0f_\u903b\u8f91\u8fd0\u7b97\u52a0\u53f7\u4e00\u4e2a\u5708","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8037.html","title":{"rendered":"\u52a0\u53f7\u7684\u516c\u5f0f_\u903b\u8f91\u8fd0\u7b97\u52a0\u53f7\u4e00\u4e2a\u5708"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u201c\u52a0\u53f7 +\u201d \u7684\u8fd0\u7b97\u539f\u7406\uff08\u201c+\u201d \u65e2\u662f\u4e00\u5143\u8fd0\u7b97\u7b26\uff0c\u4e5f\u662f\u4e8c\u5143\u8fd0\u7b97\u7b26\uff09<\/h3>\n

1. \u4e00\u5143\u8fd0\u7b97\u7b26<\/h4>\n

\u8bf4\u660e\uff1a\u2019+\u2019 \u53f7\u8fd0\u7b97\u7b26\u4f5c\u4e3a\u4e00\u5143\u8fd0\u7b97\u7b26\u65f6\uff0c\u8868\u8fbe\u5f0f\u5c06\u8fdb\u884c ToNumber()<\/code> \u64cd\u4f5c\uff08\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff09\u3002
\u6ce8\uff1a\u6709\u5173\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u53ef\u53c2\u8003 Javascript\u57fa\u7840\uff1a\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<\/font><\/p>\n

\u2460 ToNumber(argument) \u8f6c\u6362\u65b9\u5f0f\uff1a<\/h5>\n\n\n\n\n\n\n\n\n\n\n\n
argument\u7c7b\u578b<\/th>\n\u8fd4\u56de\u503c<\/th>\n<\/tr>\n<\/thead>\n
Undefined<\/td>\nreturn NaN<\/td>\n<\/tr>\n
Null<\/td>\nreturn 0<\/td>\n<\/tr>\n
Boolean<\/td>\ntrue return 1; false return 0;<\/td>\n<\/tr>\n
Number<\/td>\nreturn value<\/td>\n<\/tr>\n
String<\/td>\n\u82e5\u5b57\u7b26\u4e32\u4e3a\u7eaf\u6570\u5b57\uff0c\u8fd4\u56de\u8f6c\u6362\u540e\u7684\u6570\u5b57\uff1b\u975e\u7eaf\u6570\u5b57\u5219\u8fd4\u56de NaN<\/td>\n<\/tr>\n
Symbol<\/td>\n\u629b\u51fa TypeError \u5f02\u5e38<\/td>\n<\/tr>\n
Object<\/td>\n\u8fdb\u884c\u5982\u53f3\u6b65\u9aa4\uff1a1.\u5148\u8fdb\u884c ToPrimitive(argument, hint Number) \u5f97\u5230 rs \uff1b\u20032.\u7136\u540e\u8fd4\u56de ToNumber(rs) \u7684\u7ed3\u679c\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u2461 \u793a\u4f8b\uff1a<\/h5>\n
 \/\/ Undefined<\/span> +<\/span> undefined<\/span>;<\/span> \/\/ => NaN<\/span> \/\/ Null<\/span> +<\/span> null<\/span>;<\/span> \/\/ => 0<\/span> \/\/ Boolean<\/span> +<\/span> true<\/span>;<\/span> \/\/ => 1<\/span> +<\/span> false<\/span>;<\/span> \/\/ => 0<\/span> \/\/ String<\/span> +<\/span> '1'<\/span>;<\/span> \/\/ => 1<\/span> +<\/span> '-1'<\/span>;<\/span> \/\/ => -1<\/span> +<\/span> 'a1'<\/span>;<\/span> \/\/ => NaN<\/span> \/\/ Object<\/span> +<\/span> [<\/span>]<\/span>;<\/span> \/\/ => 0<\/span> +<\/span> { \n   <\/span>}<\/span>;<\/span> \/\/ => NaN<\/span> +<\/span> { \n   <\/span> valueOf<\/span>:<\/span> function<\/span> (<\/span>)<\/span> { \n   <\/span> return<\/span> 0<\/span> }<\/span> }<\/span>;<\/span> \/\/ => 0<\/span> \/* * \u8be5\u5bf9\u8c61\u91cd\u5199\u4e86 valueOf() \u65b9\u6cd5\uff0c * \u56e0\u6b64\u8be5\u5bf9\u8c61\u9690\u5f0f\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u8c03\u7528 valueOf() \u65b9\u6cd5\u540e\u5f97\u5230 0\uff0c * \u6545\u6700\u7ec8\u7ed3\u679c\u4e3a 0 *\/<\/span> <\/code><\/pre>\n
\n

2. \u4e8c\u5143\u8fd0\u7b97\u7b26<\/h4>\n

\u2460 \u8fd0\u7b97\u5143\u5176\u4e00\u4e3a\u5b57\u7b26\u4e32(String)<\/h5>\n

\u2003\u2003\u8fd0\u7b97\u5143\u5176\u4e00\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0c\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u64cd\u4f5c\u3002<\/em><\/p>\n

 console.<\/span>log<\/span>(<\/span>'10'<\/span> +<\/span> 1<\/span>)<\/span>;<\/span> \/\/ 101<\/span> console.<\/span>log<\/span>(<\/span>'ab'<\/span> +<\/span> 'cd'<\/span>)<\/span>;<\/span> \/\/ abcd<\/span> console.<\/span>log<\/span>(<\/span>'1'<\/span> +<\/span> true<\/span>)<\/span>;<\/span> \/\/ 1true<\/span> console.<\/span>log<\/span>(<\/span>'1'<\/span> +<\/span> undefined<\/span>)<\/span>;<\/span> \/\/ 1undefined<\/span> console.<\/span>log<\/span>(<\/span>'1'<\/span> +<\/span> null<\/span>)<\/span>;<\/span> \/\/ 1null<\/span> <\/code><\/pre>\n
\u2461 \u8fd0\u7b97\u5143\u5176\u4e00\u4e3a\u6570\u5b57(Number)<\/h5>\n

\u2003\u20031 + \u2018cd\u2019 \u4e3a\u8fd0\u7b97\u5143\u5176\u4e00\u4e3a\u5b57\u7b26\u4e32\u60c5\u51b5\uff0c\u505a\u5b57\u7b26\u4e32\u62fc\u63a5\u64cd\u4f5c\uff1b\u5176\u4f59\u4e3a\u5728\u6ca1\u6709\u5b57\u7b26\u4e32\u60c5\u51b5\u4e0b\uff0c\u8fd0\u7b97\u5143\u5176\u4e00\u4e3a\u6570\u5b57\uff0c\u505a\u7c7b\u578b\u8f6c\u6362\u540e\u505a\u6570\u503c\u76f8\u52a0\u3002<\/em><\/p>\n

 console.<\/span>log<\/span>(<\/span>1<\/span> +<\/span> 1<\/span>)<\/span>;<\/span> \/\/ 2<\/span> console.<\/span>log<\/span>(<\/span>1<\/span> +<\/span> 'cd'<\/span>)<\/span>;<\/span> \/\/ 1cd<\/span> console.<\/span>log<\/span>(<\/span>1<\/span> +<\/span> true<\/span>)<\/span>;<\/span> \/\/ 2<\/span> console.<\/span>log<\/span>(<\/span>1<\/span> +<\/span> undefined<\/span>)<\/span>;<\/span> \/\/ NaN<\/span> console.<\/span>log<\/span>(<\/span>1<\/span> +<\/span> null<\/span>)<\/span>;<\/span> \/\/ 1<\/span> <\/code><\/pre>\n

\u6ce8\uff1a\u5728\u8fd0\u7b97\u5143\u4e00\u4fa7\u4e3a\u6570\u5b57\uff0c\u53e6\u4e00\u4fa7\u4e3a\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u662f \u201c\u51cf\u53f7 - \u201d \u6216\u5176\u4ed6\u8fd0\u7b97\u7b26\uff08eg\uff1a\u201c*\u201d\u3001\u201c\/\u201d\u3001\u201c%\u201d\u3001\u201c >\u201d\u3001\u201c<\u201d\u3001\u201c==\u201d\uff09 \u7684\u8bdd\uff0c\u4f1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57<\/strong>\u8fdb\u884c\u51cf\u6cd5\u64cd\u4f5c\uff08eg\uff1a\u6570\u5b57\u76f8\u51cf\uff1a10 - \u201c1\u201d = 9\uff09\u3002<\/font><\/p>\n

\u2462 \u6570\u5b57(Number) \/ \u5b57\u7b26\u4e32(String) \u4ee5\u5916\u7684\u539f\u59cb\u7c7b\u578b\u76f8\u52a0<\/h5>\n

\u2003\u2003\u5f53\u6570\u5b57\u4e0e\u5b57\u7b26\u4e32\u4ee5\u5916\u7684\u5176\u4ed6\u539f\u59cb\u6570\u636e\u7c7b\u578b\u76f4\u63a5\u4f7f\u7528\u52a0\u53f7\u8fd0\u7b97\u65f6\uff0c\u8981\u5148\u8f6c\u4e3a\u6570\u5b57\uff08\u53ef\u53c2\u8003\u4e0a\u9762\u6709\u5173 \u201cToNumber(argument) \u8f6c\u6362\u65b9\u5f0f\u201d<\/font> \u7684\u8868\u683c\uff09\u518d\u8fd0\u7b97\uff0c\u8fd9\u4e0e\u5b57\u7b26\u4e32\u5b8c\u5168\u65e0\u5173\u3002<\/em><\/p>\n

 console.<\/span>log<\/span>(<\/span>true<\/span> +<\/span> true<\/span>)<\/span>;<\/span> \/\/ 2<\/span> console.<\/span>log<\/span>(<\/span>true<\/span> +<\/span> null<\/span>)<\/span>;<\/span> \/\/ 1<\/span> console.<\/span>log<\/span>(<\/span>true<\/span> +<\/span> undefined<\/span>)<\/span>;<\/span> \/\/NaN<\/span> console.<\/span>log<\/span>(<\/span>undefined<\/span> +<\/span> null<\/span>)<\/span>;<\/span> \/\/NaN<\/span> console.<\/span>log<\/span>(<\/span>undefined<\/span> +<\/span> undefined<\/span>)<\/span>;<\/span> \/\/NaN<\/span> console.<\/span>log<\/span>(<\/span>null<\/span> +<\/span> null<\/span>)<\/span>;<\/span> \/\/0<\/span> <\/code><\/pre>\n
\u2463 \u8fd0\u7b97\u5143\u5176\u4e00\u4e3a\u590d\u6742\u6570\u636e\u7c7b\u578b\uff08\u4ee5\u4e0b\u4e3a\u521d\u6b65\u89e3\u91ca\uff0c\u793a\u4f8b\u8bf7\u89c1\u540e\u7eed\u7b2c\u4e09\u5927\u70b9\uff09<\/h5>\n

\u6ce8\u610f\uff0c\u4ee5\u4e0a \u2460 ~ \u2462 \u7684\u793a\u4f8b\u5747\u662f\u539f\u59cb\u6570\u636e\u7c7b\u578b<\/strong>\u7684 \u201c\u52a0\u6cd5 +\u201d \u64cd\u4f5c\uff0c\u5f53\u8fdb\u884c\u590d\u6742\u6570\u636e\u7c7b\u578b<\/strong>\u7684 \u201c\u52a0\u6cd5 +\u201d \u64cd\u4f5c\u65f6\uff0cJS \u5185\u90e8\u6709\u4ee5\u4e0b\u6709\u9690\u5f0f\u8f6c\u6362<\/strong>\u8fc7\u7a0b\uff08\u5b9e\u9645\u4e0a\u662f JS \u8c03\u7528\u4e86\u5185\u90e8\u7684 toPrimitive()<\/strong> \u65b9\u6cd5\uff0c\u6709\u5173\u8be5\u65b9\u6cd5\u8be6\u89c1 JS\u539f\u59cb\u503c\u8f6c\u6362\u7b97\u6cd5\u2014toPrimitive()<\/em> \uff09\uff1a
\u2003\u2003\u2160 \u5f53 \u201c\u52a0\u53f7 +\u201d \u7684\u67d0\u4e00\u4fa7\u6570\u636e\u7c7b\u578b\u662f\u5bf9\u8c61\u65f6\uff0c\u4f1a\u5c06\u5bf9\u8c61\u5148\u8fdb\u884c valueOf()<\/strong> \u64cd\u4f5c\uff08\u6709\u5173 valuOf() \u65b9\u6cd5\u53ef\u53c2\u8003\u535a\u6587 JS \u4e2d valueOf() \u65b9\u6cd5\u7684\u8be6\u89e3<\/em>\uff09\uff1b
\u2003\u2003\u2161 \u5982\u679c\u8fd4\u56de\u7684\u662f\u539f\u59cb\u6570\u636e\u7c7b\u578b<\/strong>\uff0c\u5219\u540e\u7eed\u64cd\u4f5c\u6309\u7167\u4ee5\u4e0a\u4e09\u70b9\u8fdb\u884c\uff1b
\u2003\u2003\u2162 \u5982\u679c\u8fd4\u56de\u7684\u503c\u8fd8\u662f\u5bf9\u8c61<\/strong>\uff0c\u5219\u518d\u8c03\u7528 toString<\/strong> \u65b9\u6cd5\uff08\u6b64\u5904\u5e94\u4e86\u89e3 \u201c [] \u8c03\u7528 toString() \u65b9\u6cd5\u53d8\u6210\u7a7a\u5b57\u7b26\u4e32 \"\"<\/code>\uff0c{} \u8c03\u7528 toString() \u7b49\u4e8e [object Object]<\/code> \u201d<\/font><\/strong> \uff0c\u6709\u5173 toString \u65b9\u6cd5\u8be6\u89e3\u53ef\u53c2\u8003 \u6709\u5173 toString() \u65b9\u6cd5\u7684\u521d\u6b65\u8ba4\u8bc6<\/em> \uff09\uff1b
\u2003\u2003\u2163 \u82e5\u8fd4\u56de\u539f\u59cb\u6570\u636e\u7c7b\u578b\uff0c\u5219\u6309\u7167\u4e0a\u9762\u539f\u59cb\u6570\u636e\u7c7b\u578b\u8ba1\u7b97\uff1b
\u2003\u2003\u2164 \u5426\u5219\u62a5\u9519\u3002<\/p>\n

\u2464 \u6709\u5173 NaN \u9700\u8981\u6ce8\u610f\u7684\u70b9<\/h5>\n

\u2003\u2003\u2160. \u6267\u884c\u8fd0\u7b97\u65f6 , \u975e\u5e26 \u201c+\u201d \u53f7\u7684\u8fd0\u7b97\uff0c\u53ea\u8981\u6709 NaN \u53c2\u4e0e\uff0c\u6267\u884c\u7ed3\u679c\u5c31\u90fd\u662f NaN\uff1b
\u2003\u2003\u2161. \u5982\u679c\u662f\u5e26 \u201c+\u201d \u53f7\u7684\u8fd0\u7b97\uff0c\u4e00\u4fa7\u662f NaN\u3001\u53e6\u4e00\u4fa7\u662f\u5b57\u7b26\u4e32\u65f6\uff0c\u5c31\u6267\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u64cd\u4f5c\uff1b
\u2003\u2003\u2162. \u5982\u679c\u662f\u5e26 \u201c+\u201d \u53f7\u7684\u8fd0\u7b97\uff0c\u4e00\u4fa7\u662f NaN\u3001\u53e6\u4e00\u4fa7\u662f Number \u7c7b\u578b\u7684\u6570\u503c\u65f6\uff0c\u6267\u884c\u7ed3\u679c\u5c31\u90fd\u662f NaN\uff1b
\u2003\u2003\u2163. \u5728 JavaScript \u4e2d\u7684\u89c4\u5b9a\uff0cNaN \u8868\u793a\u7684\u662f\u975e\u6570\u5b57\uff0c\u4f46\u662f\u8fd9\u4e2a\u975e\u6570\u5b57\u4e5f\u662f\u4e0d\u540c\u7684\uff1b\u56e0\u6b64 NaN \u4e0d\u7b49\u4e8e NaN\uff0c\u4e24\u4e2a NaN \u6c38\u8fdc\u4e0d\u53ef\u80fd\u76f8\u7b49\u3002
\u6ce8\uff1aNaN \u867d\u7136\u4e0d\u662f\u4e00\u4e2a\u5177\u4f53\u6570\u503c\uff0c\u4f46\u6570\u636e\u7c7b\u578b\u786e\u662f Number \u7c7b\u578b\uff1bNaN \u548c\u4efb\u4f55 Number \u7c7b\u578b\u6570\u636e\u8fdb\u884c \u201c+\u201d\u3001\u201c-\u201d\u3001\u201c*\u201d\u3001\u201c\/\u201d\u3001\u201c% \u7b49\u64cd\u4f5c\u65f6\uff0c\u64cd\u4f5c\u7ed3\u679c\u90fd\u662f NaN\u3002<\/font><\/p>\n


\n

\u622a\u53d6\u81ea<\/strong> \u201c\u7a7a\u5bf9\u8c61 {}\u201d \u4e0e \u201c\u7a7a\u6570\u7ec4 []\u201d \u7684\u76f8\u52a0\u95ee\u9898\uff08\u8be6\u89e3\uff01\uff01\uff01\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"\u52a0\u53f7\u7684\u516c\u5f0f_\u903b\u8f91\u8fd0\u7b97\u52a0\u53f7\u4e00\u4e2a\u5708\u201c\u52a0\u53f7+\u201d\u7684\u8fd0\u7b97\u539f\u7406\uff08\u201c+\u201d\u65e2\u662f\u4e00\u5143\u8fd0\u7b97\u7b26\uff0c\u4e5f\u662f\u4e8c\u5143\u8fd0\u7b97\u7b26\uff091.\u4e00\u5143\u8fd0\u7b97\u7b26\u8bf4\u660e\uff1a\u2019+\u2019\u53f7\u8fd0\u7b97\u7b26\u4f5c\u4e3a...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8037"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8037"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8037\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}