Skip to content

Instantly share code, notes, and snippets.

@bitmage
Created June 26, 2013 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitmage/5869850 to your computer and use it in GitHub Desktop.
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.…
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
@bitmage
Copy link
Author

bitmage commented Jun 26, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment