Free-Variable

Published: 1/18/2020
Last edited: 1/26/2020

A free-variable is a global variable or variable defined externally to the function.

// window is a free variable with respect to `save`
const save = data => window.localStorage.setItem('saved', data)

You can use the free-variable partial-application pattern to fix this problem and turn this function pure:

const checkWindowExists = () => typeof window !== 'undefined'

const saveWithContext = fn => data => fn('saved', data)
// [...]

if (checkWindowExists()) {
  const save = saveWithContext(window.localStorage.setItem.bind(window.localStorage))
}

If the above seems overkill, or needless, just remember that a runtime error can crash everything in JS land and a free benefit of the pattern is that you get the ability to inject mocks into your tests (e.g. const save = saveWithContext(jest.fn()) for instance). So pick your battles.

Published: 1/18/2020
Last edited: 1/26/2020
See this page on Github
Built with 💛 by Open Sorcerers
Created with Gatsby using the 😎 foresight starter