equals - Is the JavaScript operator === provably transitive? - Stack Overflow

JavaScript's quirky weakly-typed == operator can easily be shown to be non-transitive as follows:v

JavaScript's quirky weakly-typed == operator can easily be shown to be non-transitive as follows:

var a = "16";
var b = 16;
var c = "0x10";
alert(a == b && b == c && a != c); // alerts true

I wonder if there are any similar tricks one can play with roundoff error, Infinity, or NaN that could should show === to be non-transitive, or if it can be proved to indeed be transitive.

JavaScript's quirky weakly-typed == operator can easily be shown to be non-transitive as follows:

var a = "16";
var b = 16;
var c = "0x10";
alert(a == b && b == c && a != c); // alerts true

I wonder if there are any similar tricks one can play with roundoff error, Infinity, or NaN that could should show === to be non-transitive, or if it can be proved to indeed be transitive.

Share Improve this question asked Oct 1, 2011 at 20:11 Ray ToalRay Toal 88.5k20 gold badges184 silver badges243 bronze badges 8
  • 1 Are you asking about JavaScript as defined by any particular ECMAScript version, or JavaScript as implemented in any particular browser? – zzzzBov Commented Oct 1, 2011 at 20:14
  • 3 @Ray == isn't transitive because of type coercion. === doesn't coerce... – Šime Vidas Commented Oct 1, 2011 at 20:17
  • @zzzzBov I was thinking of ECMAScript 5 but any version of JavaScript will do. – Ray Toal Commented Oct 1, 2011 at 20:18
  • @Šime Vidas I know === does not coerce but I'm not sure that that constitutes a proof. It very well might.... – Ray Toal Commented Oct 1, 2011 at 20:20
  • 1 Cheating: jsfiddle/JFVcG/1. – pimvdb Commented Oct 1, 2011 at 20:46
 |  Show 3 more ments

3 Answers 3

Reset to default 3

The === operator in Javascript seems to be as transitive as it can get.

NaN is reliably different from NaN:

>>> 0/0 === 0/0
false
>>> 0/0 !== 0/0
true

Infinity is reliably equal to Infinity:

>>> 1/0 === 1/0
true
>>> 1/0 !== 1/0
false

Objects (hashes) are always different:

>>> var a = {}, b = {};
>>> a === b
false
>>> a !== b
true

And since the === operator does not perform any type coercion, no value conversion can occur, so the equality / inequality semantics of primitive types will remain consistent (i.e. won't contradict one another), interpreter bugs notwithstanding.

If you look at the spec (http://bclary./2004/11/07/#a-11.9.6) you will see that no type coercion is being made. Also, everything else is pretty straightforward, so maybe only implementation bugs will make it non-transitive.

Assuming you have variable a,b, and c you can't be 100% certain as shown here. If someone is doing something as hackish as above in production, well then you probably have bigger problems ;)

var a = 1;
var b = 1;
Object.defineProperty(window,"c",{get:function(){return b++;}});

alert(a === b && b === c && a !== c); // alerts true

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742368127a4430724.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信