Created
June 26, 2013 18:11
-
-
Save bitmage/5869850 to your computer and use it in GitHub Desktop.
Inside the node.js EventEmitter implimentation, the === operator is used within removeListener to determine which listener should be removed. I wanted to test if it remains true to the actual function instance. Here is an example where functions are created in a loop, but each one has a different closure and therefore returns a different result.…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collector = [] | |
compare = (target) -> | |
-> | |
console.log 'in timeout, collector length:', collector.length | |
for fn, i in collector | |
console.log "#{i}:", (fn is target) | |
for i in [1..3] | |
do (i) -> | |
foo = -> i | |
collector.push foo | |
if i is 1 | |
console.log 'setting timeout' | |
setTimeout compare(foo), 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Conclusion: The comparison function correctly determines that the first function is the one we are referring to. So the identity equality operator is effective at distinguishing between the same function defined in different contexts.