0

I am building an algorithm in typescript/ionic 2 that requires a function hierarchy (functions within functions). I am 99% sure that there is no way to avoid having nested functions because of the google API (long explanation required, just trust me on that)

I will use a simple demo that illustrates my problem to avoid posting 300 lines of code.

The problem involves accessing and manipulating global variables within the nested functions. I need a way of setting the this.secondvar in the secondSet function. Are there any ways to achieve this?

export class HomePage {

  public firstVar = [];
  public secondvar;
  constructor(public navCtrl: NavController) {

  }

  ionViewDidLoad(){
    this.setVar();
  }

  setVar(){
    this.firstVar.push("hello", "goodbye");
    console.log();
    getVar();

    function getVar(){
      //console.log(this.lol); unable to access
      secondSet();

      function secondSet(){
        console.log("test")
        //this.secondVar = "hello" //how do i set this ?
      }
    }
  }
}

3 Answers 3

2

Use Arrow functions which takes the scope of the class.It will not have its own this value.

setVar(){
 this.firstVar.push("hello", "goodbye");
 console.log();

let getVar=()=>{
  //console.log(this.lol); unable to access

  let secondSet=()=>{
    console.log("test")
    //this.secondVar = "hello" //how do i set this ?
  }
        secondSet();

}
     getVar();
}

what is the reasoning behind the positioning of the function calls? Why are they called after the function is declared?

It is because of the use of let.let variable declaration is block scoped. According to the docs:

property of block-scoped variables is that they can’t be read or written to before they’re actually declared. While these variables are “present” throughout their scope, all points up until their declaration are part of their temporal dead zone.

So:

     secondSet();//throws error

  let secondSet=()=>{
    console.log("test")
    //this.secondVar = "hello" //how do i set this ?
  }
Sign up to request clarification or add additional context in comments.

2 Comments

"ts cannot find name "getVar""
Thanks. One question, what is the reasoning behind the positioning of the function calls? Why are they called after the function is declared?
1

Use context variable. Assign value of "this" to a local variable say "context". use "context" variable as "this".

export class HomePage {

  public firstVar = [];
  public secondvar;
  constructor(public navCtrl: NavController) {

  }

  ionViewDidLoad(){
    this.setVar();
  }

  setVar(){
     var context = this;
    this.firstVar.push("hello", "goodbye");
    console.log();
    getVar();

    function getVar(){
      //console.log(this.lol); unable to access
      secondSet();

      function secondSet(){
        console.log("test")
        context.secondVar = "hello" // This is how you access the secondVar 
      }
    }
  }
}

Comments

0

You can define your inner functions to be arrow functions (lambdas), which will preserve the context of 'this'. So something like this:

var secondSet = () => { this.secondVar = 'whatever' };

If you look at the compiled javascript, it automatically saves the value of 'this' to a variable and uses that in the lambda.

2 Comments

How do i call that function
It's a function like any other so just secondSet(). Or any of the other ways you can call a function (call, apply).

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.