gpt4 book ai didi

javascript - javascript 运算符 === 和 lodash 方法 _.eq() 之间的区别?

转载 作者:行者123 更新时间:2023-11-29 17:50:14 26 4
gpt4 key购买 nike

javascript 中的 === 比较器和 lodash 中的 _.eq() 方法有区别吗?

_.eq() ( Link )

Performs a SameValueZero comparison between two values to determine if they are equivalent.

更新

不确定这是否检查相同的类型,例如 === 运算符。

7.2.10SameValueZero (x, y)#

The internal comparison abstract operation SameValueZero(x, y), where x and y are ECMAScript language values, produces true or false. Such a comparison is performed as follows:

If Type(x) is different from Type(y), return false. If Type(x) is Number, then If x is NaN and y is NaN, return true. If x is +0 and y is -0, return true. If x is -0 and y is +0, return true. If x is the same Number value as y, return true. Return false. Return SameValueNonNumber(x, y). NOTE SameValueZero differs from SameValue only in its treatment of +0 and -0.

7.2.11SameValueNonNumber (x, y)#

The internal comparison abstract operation SameValueNonNumber(x, y), where neither x nor y are Number values, produces true or false. Such a comparison is performed as follows:

Assert: Type(x) is not Number. Assert: Type(x) is the same as Type(y). If Type(x) is Undefined, return true. If Type(x) is Null, return true. If Type(x) is String, then If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false. If Type(x) is Boolean, then If x and y are both true or both false, return true; otherwise, return false. If Type(x) is Symbol, then If x and y are both the same Symbol value, return true; otherwise, return false. Return true if x and y are the same Object value. Otherwise, return false.

最佳答案

Loadash _.eq 也会检查 NaN 是否相等。

_.eq(NaN, NaN) => true

NaN === NaN => false

https://github.com/lodash/lodash/blob/6cb3460fcefe66cb96e55b82c6febd2153c992cc/eq.js#L32

function eq(value, other) {
return value === other || (value !== value && other !== other)
}

关于javascript - javascript 运算符 === 和 lodash 方法 _.eq() 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44624058/

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