-2

I found out I have two functions the share some some code so I decided to put it insied a template function:

function template(callback){
  var all, these, variables, are, used, inthe, callbackFunction;
  for (var i=0; i<10; i++){
      callback();
  }
}
function myFirstFunction(){
   //do something with all those variables
}
function mySecondFunction(){
   //do something else
}

So for each function I call template(myFirstFunction) and template(mySecondFunction)

Is there any way I can use all variables defined in template function from my functions without passing them by parameter?


EDIT:

My functions are actually methods of an object:

function MyObject(){

};
MyObject.prototype.template = function(){ 
  var all, these, variables, are, used, inthe, callbackFunction;
  for (var i=0; i<10; i++){
    callback();
  }};
MyObject.prototype.myFirstMethod = function(){ 
    this.template(function(){
        //doSomething with all those variables
    });
};
 MyObject.prototype.mySecondMethod = function(){ 
    this.template(function(){
        //doSomething else
    });
};
Community
  • 1
  • 1
de3
  • 1,890
  • 5
  • 24
  • 39
  • 5
    move them outside of template and you're done – David Nguyen Mar 02 '16 at 16:07
  • 1
    You can either move them outside the template function and give them global scope or you can pass them as parameters. – stackErr Mar 02 '16 at 16:08
  • 1
    You *could* set them as properties of `this` (or a context object) and then use `callback.call(this)`; – Davin Tryon Mar 02 '16 at 16:08
  • Thanks, I have edited the question. I now how scopes work. Is just that I am working with an object. And I was wondering if there was anything I could do other than putting them as global variables or passing them by parameter. Apparently not – de3 Mar 02 '16 at 16:17
  • The best way to avoid *polluting the global namespace*, is to wrap everything inside a *self-executing function*. See my updated answer (http://stackoverflow.com/questions/35752338/accessing-variables-outside-function-call/35752443#35752443) for how to do that for your object. – John Slegers Mar 02 '16 at 16:22
  • I'm voting to close this question as off-topic because it's stupid question I asked in a bad moment and it hurts my reputation whenever I have to share my stackoverflow account – de3 Oct 31 '18 at 20:40

3 Answers3

8

Woo! Scope problem! You need to declare the variables outside the local scope of the function. Like this:

var all, these, variables, are, used, inthe, callbackFunction;
function template(callback){
  for (var i=0; i<10; i++){
      callback();
  }
}
function myFirstFunction(){
   //do something with all those variables
}
function mySecondFunction(){
   //do something else
}

You can then access them in each of those functions. Scope can be tricky if you are new and I suggest reading up on it.

millerbr
  • 2,951
  • 1
  • 14
  • 25
0

If you want to prevent polluting the global namespace, you could wrap everything inside a self-executing function :

var MyObject = (function() {
    var all, these, variables, are, used, inthe, callbackFunction;
    
    var o = function() {
        // Do stuff
    };
    
    o.prototype.template = function(){ 
        for (var i=0; i<10; i++) {
            callback();
        }
    };
      
    o.prototype.myFirstMethod = function(){ 
        this.template(function() {
            // Do something with all those variables
        });
    };

    o.prototype.mySecondMethod = function(){ 
        this.template(function() {
            // Do something else
        });
    };
    
    return o;
})();

Further reading :

Implementing Private and Protected Members in JavaScript

Community
  • 1
  • 1
John Slegers
  • 45,213
  • 22
  • 199
  • 169
0

Myself, I'd keep them in one place, without polluting the scope:

MyObject.prototype.template = function(){ 
    this.params = {all: '', these: '', variables: '', etc: ''}
    for (var i=0; i<10; i++){
        callback();
}};

MyObject.prototype.myFirstMethod = function(){ 
    var self = this;
    this.template(function(){
        console.log(self.params);
    });
};
eithed
  • 3,933
  • 6
  • 40
  • 60