繁体   English   中英

javascript中的函数之间共享变量

[英]Sharing variables between functions in javascript

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

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

现在我想要我的第二个方法 Method2,它是从 jsFile1.js 内部调用的,它也能够使用从 anotherJsFile 发送到方法 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()
    }}

只需编写一个采用 integer 的方法并在构造函数中导入 class

文件1.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()
    }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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