簡體   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