How would I get a positive test for bar and foo's equality?
foo = function() { a = 1; }; bar = function() { a = 1; }; if (foo === bar) alert('baz'); if (foo == bar) alert('qux');
Both the above conditionals are false.
Updated - As requested the reason I need to test for function equality
I am building a Publish/Subscribe framework and need to pass the callback inorder to unsubscribe to a topic.
Please see the fiddle: http://jsfiddle.net/jamiefearon/hecMS/47/
== is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.
Two functions are equal if they have the same domain and codomain and their values are the same for all elements of the domain.
JavaScript provides 3 ways to compare values: The strict equality operator === The loose equality operator == Object.is() function.
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
You could check whether the content of two functions is exactly the same by comparing the result of calling toString
on them
var foo = function() { a = 1; }; var bar = function() { a = 1; }; alert(foo.toString() == bar.toString());
That will, however, fail if there is but one character that is different. Checking to see if two functions do the same thing, on the other hand, is pretty much impossible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With