{"id":251335,"date":"2022-09-07T08:01:38","date_gmt":"2022-09-07T00:01:38","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=251335"},"modified":"2022-08-29T09:02:33","modified_gmt":"2022-08-29T01:02:33","slug":"lodash-to-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/lodash-to-linux.html","title":{"rendered":"lodash\u91ccto\u7cfb\u5217\u4e4b\u5c06\u6570\u636e\u8f6c\u6362\u6210\u6570\u5b57\u7c7b\u578b\u5b9e\u73b0\u793a\u4f8b\u8be6\u89e3"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86lodash\u91ccto\u7cfb\u5217\u4e4b\u5c06\u6570\u636e\u8f6c\u6362\u6210\u6570\u5b57\u7c7b\u578b\u5b9e\u73b0\u793a\u4f8b\u8be6\u89e3\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u6b63\u6587<\/strong><\/div>\n \u5728lodash\u91cc\u7684to\u7cfb\u5217\u91cc\uff0c\u5c06\u76ee\u6807\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u7684\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4e86toNumber\u65b9\u6cd5\u3001toFinit\u65b9\u6cd5\u3001toInteger\u65b9\u6cd5\uff0ctoSafeInteger\u65b9\u6cd5\uff0c\u4e0b\u9762\u6765\u770b\u770b\u5404\u4e2a\u65b9\u6cd5\u7684\u4f7f\u7528\u548c\u5b9e\u73b0\u3002<\/p>\n toNumber<\/strong><\/div>\n toNumber\u65b9\u6cd5\u4e3b\u8981\u662f\u5c06\u53c2\u6570value\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n \u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n toNumber(3.2)\r\n\/\/ => 3.2\r\ntoNumber(Number.MIN_VALUE)\r\n\/\/ => 5e-324\r\ntoNumber(Infinity)\r\n\/\/ => Infinity\r\ntoNumber('3.2')\r\n \/\/ => 3.2<\/pre>\n \u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n import isObject from '.\/isObject.js'\r\nimport isSymbol from '.\/isSymbol.js'\r\nconst NAN = 0 \/ 0\r\nconst reTrim = \/^\\s+|\\s+$\/g\r\nconst reIsBadHex = \/^[-+]0x[0-9a-f]+$\/i\r\nconst reIsBinary = \/^0b[01]+$\/i\r\nconst reIsOctal = \/^0o[0-7]+$\/i\r\nconst freeParseInt = parseInt\r\nfunction toNumber(value) {\r\n if (typeof value === 'number') {\r\n return value\r\n }\r\n if (isSymbol(value)) {\r\n return NAN\r\n }\r\n if (isObject(value)) {\r\n const other = typeof value.valueOf === 'function' ? value.valueOf() : value\r\n value = isObject(other) ? `${other}` : other\r\n }\r\n if (typeof value !== 'string') {\r\n return value === 0 ? value : +value\r\n }\r\n value = value.replace(reTrim, '')\r\n const isBinary = reIsBinary.test(value)\r\n return (isBinary || reIsOctal.test(value))\r\n ? freeParseInt(value.slice(2), isBinary ? 2 : 8)\r\n : (reIsBadHex.test(value) ? NAN : +value)\r\n}<\/pre>\n |