Sharing variables between functions in javascript

295 Views Asked by At

if this question is not clear, let me know and I will exapand on it. Not great with JS.

I have a js file. let's call it jsFile1.js in which I have two methods. Method1 is being called from another file (anotherJsFile.js) and that call sends a variable to the Method1 in jsFile1.js.

Now I want my second method, Method2, which is being called from inside jsFile1.js to also be able to use the variable sent from anotherJsFile to method 1.

Have tried using id's and set value etc but it won't work. Any suggestions? Presume I have to store const tmp in the config or init and then access it from the Method2?

File1

Method1(item, table) {
//item is a marked item from the table, table contains all entries
const tmp = {table, id: "tmpTable"};
}

Method2() {
const data = this.$$("tmpTable").getValues();
}

config() {
    const Method2Button = {
        view:"button",
        label:"Method2",
        click: () => this.Method2()
    }}
1

There are 1 best solutions below

0
Lucas.Feichtinger On

Just write a method that takes an integer and import the class in you constructor

File1.js

private test: any

Method1(item, table) {
  const tmp = {table, id: "tmpTable"};
  this.test = tmp
}

Method2() {
  return this.test.id //.getValues() ?? 
  // return this.class.getValues(this.test.id)
}

config() {
    const Method2Button = {
        view:"button",
        label:"Method2",
        click: () => this.Method2()
    }}