首页 > 【lodash源码】对toNumber的一些疑问

【lodash源码】对toNumber的一些疑问

在拜读lodash时对作者的一些意图不太明白,前来请教。下面贴代码:

function toNumber (value) {
    if (typeof value == 'number') return value;
    if (isSymbol(value)) return NAN;
    if (isObject(value)) {
        var other = isFunction(value.valueOf) ? value.valueOf () : value;
        value = isObject(other) ? (other + '') : other;
    }
    if (typeof value != 'string') {
        return value === 0 ? value : +value;
    }
    value = value.replace (reTrim, '');
    var isBinary = reIsBinary.test (value);
    return (isBinary || reIsOctal.test (value))
        ? freeParseInt (value.slice (2), isBinary ? 2 : 8)
        : (reIsBadHex.test (value) ? NAN : +value);
}

下面是toNumber的一些实例:

    * _.toNumber(3.2);
    * // => 3.2
    *
    * _.toNumber(Number.MIN_VALUE);
    * // => 5e-324
    *
    * _.toNumber(Infinity);
    * // => Infinity
    *
    * _.toNumber('3.2');
    * // => 3.2

源码中有这么一段:

if (isObject(value)) {
        var other = isFunction(value.valueOf) ? value.valueOf () : value;
        value = isObject(other) ? (other + '') : other;
    }

先判断是不是object, 接着判断是不是function, 传入的参数是 value.valueOf。如果一开始传入的就是一个funciton, 那么other也将是一个function. 接下来对other进行isObject判断之后进行 + '' 操作。这样做有什么意义, 为什么不直接在得知是function之后返回 NAN ?

还有这一段:

var isBinary = reIsBinary.test (value);
return (isBinary || reIsOctal.test (value))
        ? freeParseInt (value.slice (2), isBinary ? 2 : 8)
        : (reIsBadHex.test (value) ? NAN : +value);

其中:

 /** Used to detect binary string values. */
//判断2进制
var reIsBinary = /^0b[01]+$/i;

/** Used to detect octal string values. */
//判断8进制
var reIsOctal = /^0o[0-7]+$/i;

 /** Used to detect bad signed hexadecimal string values. */
//判断16进制
var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;

为什么在判断16进制时 是带有[-+]的?作者为什么对16进制的注释是 bad signed

【热门文章】
【热门文章】