简体   繁体   English

javascript中的函数之间共享变量

[英]Sharing variables between functions in javascript

if this question is not clear, let me know and I will exapand on it.如果这个问题不清楚,请告诉我,我会详细说明。 Not great with JS. JS不太好。

I have a js file.我有一个 js 文件。 let's call it jsFile1.js in which I have two methods.我们称它为 jsFile1.js,其中我有两种方法。 Method1 is being called from another file (anotherJsFile.js) and that call sends a variable to the Method1 in jsFile1.js. Method1 正在从另一个文件 (anotherJsFile.js) 中调用,并且该调用将一个变量发送到 jsFile1.js 中的 Method1。

Now I want my second method, Method2, which is being called from inside jsFile1.js to also be able to use the variable sent from anotherJsFile to method 1.现在我想要我的第二个方法 Method2,它是从 jsFile1.js 内部调用的,它也能够使用从 anotherJsFile 发送到方法 1 的变量。

Have tried using id's and set value etc but it won't work.尝试过使用 id 和设置值等,但它不起作用。 Any suggestions?有什么建议么? Presume I have to store const tmp in the config or init and then access it from the Method2?假设我必须将 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()
    }}

Just write a method that takes an integer and import the class in you constructor只需编写一个采用 integer 的方法并在构造函数中导入 class

File1.js文件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