This might be a dumb question, but I can't use google because of all the "Closure 101" links ...
In general, if we take into account the duplicated code that relies on the closing context, is there a way to remove the code in the function call, while remaining a new function, rely only on closing, and not pass everything that is needed through the parameters?
An example of rough code might look like this:
function doWork(){ // initialize variables // manipulate variables // ... $.ajax({ //... success: function(data){ // DUPLICATE CODE INSTANCE 1 HERE // RELIES ON VARIABLES IN THE CLOSURE } }); // More code $.ajax({ //... success: function(data){ // DUPLICATE CODE INSTANCE 2 HERE // RELIES ON VARIABLES IN THE CLOSURE } }); }
As far as I know, if I remove the logic of success blocks in
function onSuccess(...){
Then onSuccess is no longer part of the closure, so all closure variables passed as parameters where the current logic uses closure to access will be required.
Am I really wrong about how closures work? Is there a way to "pass a closure" to the onSuccess function, rather than pass individual variables?
source share