1

I have the following code below, but one problem. I want to access a variable from inside the function clslevel() named the_id, from the calling anonymous function. I tried this.the_id but it returns undefined.

function clslevel(id){ 
  var the_id = id;
  this.methodOne=function(param){
    param();
    return this;
  };
  this.methodTwo=function(param){
    param();
    return this;
  };
  
}


function level(id){
  return new clslevel(id);
}



level("myButton")
  .methodOne(
    function(){
      console.log("methodOne called.");
      // how can I access the variable 'the_id' in clslevel() from here?
    }  
  )
  .methodTwo(
    function(){
      console.log("methodTwo called");
    }  
  )

thank you in advance!

1
  • 1
    You can't. Absolutely. That's how scope works. What problem are you actually trying to solve? Commented Nov 2, 2018 at 12:44

3 Answers 3

2

Pass it as a parameter to the function, like this:

function clslevel(id){ 
  var the_id = id;
  this.methodOne=function(param){
    param(the_id);
    return this;
  };
  this.methodTwo=function(param){
    param();
    return this;
  };
  
}


function level(id){
  return new clslevel(id);
}



level("myButton")
  .methodOne(
    function(the_id){
      console.log("methodOne called.", the_id);
      // You have the_id here
    }  
  )
  .methodTwo(
    function(){
      console.log("methodTwo called");
    }  
  )

Sign up to request clarification or add additional context in comments.

Comments

2

You can pass this variable into callback:

function clslevel(id){ 
  var the_id = id;
  this.methodOne=function(param){
    param(the_id);
    return this;
  };
  this.methodTwo=function(param){
    param();
    return this;
  };

}


function level(id){
  return new clslevel(id);
}



level("myButton")
  .methodOne(
    function(passed_id){
      console.log("methodOne called.");
      console.log(passed_id)
      // how can I access the variable 'the_id' in clslevel() from here?
    }  
  )
  .methodTwo(
    function(){
      console.log("methodTwo called");
    }  
  )

Comments

1

You can pass a reference of your object so you can use the parent functions inside the other scope

function clslevel(id){ 
  this.the_id = id;
  this.methodOne=function(param){
    param(this);
    return this;
  };
  this.methodTwo=function(param){
    param(this);
    return this;
  };
  
}


function level(id){
  return new clslevel(id);
}



level("myButton")
  .methodOne(
    function(parent){
      console.log("methodOne called.");
      console.log('the_id = ' + parent.the_id)
      // how can I access the variable 'the_id' in clslevel() from here?
    }  
  )
  .methodTwo(
    function(){
      console.log("methodTwo called");
    }  
  )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.