Skip to content

Instantly share code, notes, and snippets.

@jamischarles
Created January 28, 2021 19:02
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 jamischarles/ce3db5dc5be5a55f31756e2d09da095d to your computer and use it in GitHub Desktop.
Save jamischarles/ce3db5dc5be5a55f31756e2d09da095d to your computer and use it in GitHub Desktop.
Closure experiments
// weirdness with closures
// Setup: pass an obj to a function, and then another function (in my case apollo server)
// Test: the thing is executed, then I change the object, then execute again. What is the object value passed in?
var mockReqObj = { test: 'jamis' };
// Variation 1)
// appears to use default value in console log (for both)
mockReqObj = { test: 'NBODOy' };
// Variation 2)
// appears to use new value in console log (for both) (have to test more to see if it's just the logging delay that's showing it wrong)
mockReqObj.test = 'NBODOy';
// Variation 3) Safest?
// Pass in a function instead of obj that gets the obj when it's needed (I assume this will be most up to date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment