2012-02-26 110 views

回答

4

已提出明确in the source

function eq(a, b, stack) { 
    // Identical objects are equal. `0 === -0`, but they aren't identical. 
    // See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal. 
    if (a === b) return a !== 0 || 1/a == 1/b; 

事实上,JavaScript并解释0-0differently,但你平时看不到这一点,因为0 == -00 === -0。只有a few ways来检查差异。

+0

谢谢!对我而言,这似乎是一个非明显的设计决定,因为'==='几乎总是适合我的需求。但我明白了。 – brahn 2012-02-26 17:16:12

3

eq功能here的来源。 -1 * 0-0,而不是0,因此根据isEqual0-0不相等。

相关线路:

// Identical objects are equal. `0 === -0`, but they aren't identical. 
// See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal. 
if (a === b) return a !== 0 || 1/a == 1/b; 

我也知道在此之前,但它会做一些有趣的邪恶代码。

2

它比这更深。 JavaScript使用IEEE双精度浮点数字,它们对0和-0有不同的表示(当你处理限制等时,这可能很重要)。但通常你不会注意到这一点,因为0 === -0。