var x = function() {};
var y = function() {};
alert(x === y); // is false;
Why is x not equal to y if they are both initialised to the same value?
When you compare objects in JavaScript, you are checking to see if they are the same object, not if they are identical objects.
same and identical until I saw the MDN again. Off course, your explanation it absolutly correct.From MDN:
If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
Clearly, your objects are distinct from each other, and refer to different memory locations. The equals comparison operator checks if both operands refer to the same object, not if they are are replicas.
Consider the fact that (new Number(1)) != (new Number(1)), whereas 1 == 1
refer to the ame object in memory part when using === on objects. For some reason I assumed that because they both were empty functions it would be different. Even after working with Javascript now for a while I still get cought out by the basics some times. Thank you for the MDN, which I should be remembering by now and the short sample.same objects in memeory in the MDN which made the penny drop.The ECMA standard gives some precise rules on how strict equality works in JavaScript. Basically, as @Quentin said, if you are comparing two objects (other than Number, String, Boolean, null or undefined), it only returns true if they are the same object. That is not the case here.
Consider this code:
var x = function() {};
var y = function() {};
x.something = "this is x";
y.something = "this is not x";
alert(x.something === y.something);
This will give false.
From Spec-11.9.6:
The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
1.If Type(x) is different from Type(y), return false.
2.If Type(x) is Undefined, return true.
3.If Type(x) is Null, return true.
...
7.Return true if x and y refer to the same object. Otherwise, return false.
function(){}thatx === yreturn false. No framework has anything to do with that. I edited my question so it is simpler. I supppose it has nothing to do with the fact I'm unit testing i guess :)onChange. That object is used in another Javascript file I'm writing tests against. During the initialisation method of that other file I'm testing theonChangeproperty is set to a function. Passing a mock of the object I initialisedonChangein the mock tofunction(){}and wanted to test that after initialise has been executedonChangeis not stillfunction(){}..toString(). Could you then not doif (x.toString() === y.toString()) ...