How to access variables inside a function from outside in another function in typescript?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to access variables inside a function from outside in another function in typescript?



In my html page onchange, im just calling a method by passing parameters. After retrieving the value, onclick of another button Im calling function2. I need to use the variable from function in function2 in the same class. Im using angular 6 & typescript. My ts file is given below.


export class component extends Lifecycle
cc1: string;
constructor ()
Function (cc1,cc2)

this.cc1 = cc1;
// return cc1;


Function2 ()
console.log(cc1);




Can somebody help me with this?




3 Answers
3



use this keyword inside any method to access to any property or method related to class.


this


export class component extends Lifecycle
cc1: string;
constructor()
super();

Function(cc1, cc2)
this.cc1 = cc1;
// return cc1;


Function2()
console.log(this.cc1);




You have to call super if you extend the class


super



In Function2, you would use this.cc1 as shown below (note that I renamed the parameters passed to Function1 to clarify their difference from the class-level variable)


Function2


this.cc1


Function1


export class component extends Lifecycle
cc1: string;

constructor ()
super();


Function1 (_cc1, _cc2)
this.cc1 = _cc1;


Function2 ()
console.log(this.cc1);




In Function 2


Function2 ()
console.log(this.cc1);

}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS