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



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

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

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

我试过使用id和设置值等,但它不起作用。有什么建议吗?假设我必须将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()
}}

最新更新