Moves generic js code to functions prevented by closure?

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?

+4
source share
3 answers

You are not mistaken about closing behavior. What you can do is declare the onSuccess function inside doWork .

 function doWork(...) { function onSuccess(...) { // ... } $.ajax({ //... success: onSuccess }); $.ajax({ //... success: onSuccess }); } 
+4
source

If you do not define it in closing

 function doWork(){ // initialize variables // manipulate variables // ... function onSuccess(data){ // DUPLICATE CODE INSTANCE 2 HERE // RELIES ON VARIABLES IN THE CLOSURE } $.ajax({ //... success: onSuccess }); // More code $.ajax({ //... success: onSuccess }); } 
+2
source

What you can do is turn the things you need into the equivalent of JavaScript public using this.var . You can then pass the this link to an object outside the scope of your closure, and access the properties and closure methods you passed (from this ). Take the following code, for example:

Edited from the @danp manual:

 var Work = function Work() { this.closureVar = "hello"; this.closureOnSuccess = function () { console.log("Inner call:" + this.closureVar); } this.someCall = function() { globalOnSuccess(this); //Give the global function a reference to this closure this.closureOnSuccess(); } this.someCall(); } var globalOnSuccess = function (context) { //context is a reference to a closure console.log("Outer call:" + context.closureVar); }; //Globally scoped function var work = new Work(); 

jsFiddle

And another example:

 var Work = function Work() {}; Work.prototype = { closureVar: "hello", closureOnSuccess: function () { console.log("Inner call:" + this.closureVar); }, someCall: function () { globalOnSuccess(this); this.closureOnSuccess(); } }; var globalOnSuccess = function (context) { console.log("Outer call:" + context.closureVar); }; var work = new Work(); work.someCall(); 

jsFiddle

0
source

All Articles