Master the JavaScript Interview: What is a Closure?
Eric Elliott
1.3K44

Thanks for this article Eric — it led to some worthwhile (albeit sobering) realizations about my understanding of closures.

While I was playing around with this material, I refactored your partialApply() example on my own to use only spread operators, without the need for .apply():

function partialApply(fn, ...fixedArgs) {
return (...remainingArgs) => fn(...fixedArgs, ...remainingArgs)
}

Looking at it this way made me wonder— would I have similar results without the spread operator? My understanding of the scope chain led me to assert that the innermost function already has a this context in common with its outer lexical environment.

function partialApply(fn, fixedArgs) {
return (remainingArgs) => fn(fixedArgs, remainingArgs)
}

Indeed, this partial application still works without a spread operator. I cloned your bin to try running the tests, and sure enough they come back OK: http://jsbin.com/votiwet/edit

My question for you , then — if both cases produce the same result, what nuance does calling apply() or using  provide for us that the ‘plain’ implementation does not?

Show your support

Clapping shows how much you appreciated Zachary Collier’s story.