【问题标题】:Sharing variables between functions in javascript在javascript中的函数之间共享变量
【发布时间】:2021-05-28 14:13:29
【问题描述】:

如果这个问题不清楚,请告诉我,我会详细说明。不适合 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()
    }}

【问题讨论】:

  • 你创建一个简单的 store.js 文件,您可以在其中创建一个变量和获取和更新变量的 getter、setter 函数。那么您不必将其传递给Method1Method1Method2 都可以从 store.js 文件本身获取值。您只需要更新 store.js 中的值,一切都会正常
  • 然而我想存储它初始化或配置的变量而不是另一个文件。

标签: javascript typescript variables frontend webix


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2017-03-27
    • 2016-03-25
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多