简体   繁体   中英

Sharing variables between functions in javascript

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()
    }}

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()
    }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM