在javascript中的函数之间共享变量

问题描述

如果这个问题不清楚,请告诉我,我会详细说明。不太适合 JS。

我有一个 js 文件。让我们称它为 jsFile1.js,其中我有两种方法。正在从另一个文件 (anotherJsFile.js) 调用 Method1,该调用一个变量发送到 jsFile1.js 中的 Method1。

现在我想要我的第二个方法 Method2,它从 jsFile1.js 内部调用,也能够使用从 anotherJsFile 发送到方法 1 的变量。

曾尝试使用 id 和 set value 等,但它不起作用。有什么建议么?假设我必须将 const tmp 存储在 config 或 init 中,然后从 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()
    }}

解决方法

只需编写一个接受整数的方法并在构造函数中导入该类

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