gpt4 book ai didi

javascript - 为什么 lodash .isNumber 函数比 typeof value == 'number' 更复杂

转载 作者:行者123 更新时间:2023-12-05 01:12:28 26 4
gpt4 key购买 nike

洛达什 isNumber函数有一个额外的条件来检查值是否为数字。我不确定为什么需要这样做,在什么情况下仅使用 typeof value == 'number' 是不够的

function isNumber(value) {
return typeof value == 'number' ||
(isObjectLike(value) && getTag(value) == '[object Number]')
}

https://github.com/lodash/lodash/blob/aa1d7d870d9cf84842ee23ff485fd24abf0ed3d1/isNumber.js

最佳答案

从您的链接:

Checks if value is classified as a Number primitive or object.


var n = new Number(3);
console.log(typeof n); // "object"
console.log(_.isNumber(n)); // true

MDN - Number :

The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor. A primitive type object number is created using the Number() function.

关于javascript - 为什么 lodash .isNumber 函数比 typeof value == 'number' 更复杂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56663938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com