在另一个函数中使用变量中的数据。

huangapple go评论68阅读模式
英文:

Use data from variable in another function

问题

我有一个类似这样的函数(value来自选择):

function getSelectValuePower () {
var power = document.getElementById("power").value;
sessionStorage.setItem("power", power);
console.log(power);
}


如何在另一个函数中使用变量power的值?
例如:

我想在这里使用它:

function getValue () {
var number = 10;
var selectedValue = number + power;
console.log(selectedValue);
}


所以,当我的power为7时,getValue()中的结果应为17。

我尝试使用全局变量来实现,但不起作用。
英文:

I've got a function like this (value is from select):

function getSelectValuePower () {
 var power = document.getElementById("power").value;
  sessionStorage.setItem("power", power);
  console.log(power);
}

How can I use value from variable power in another function ?
For example :

I want to use it here :

function getValue () {
  var number = 10;
  var selectedValue = number + power;
  console.log(selectedValue2);
}

So, when my power is 7, the result in getValue() should be 17.

I tried to do it with global variables, but doesn't work.

答案1

得分: 4

获取第二个函数中的权力:

var power = sessionStorage.getItem("power");
英文:

Get power in second function:

var power = sessionStorage.getItem("power");

答案2

得分: 1

你也可以使用 parseInt()

英文:

you can also use parseInt() as well

function getValue() {
  const number = 10;
  const power = parseInt(sessionStorage.getItem("power")) || 0;
  const selectedValue = number + power;
  console.log(selectedValue);
}

答案3

得分: 1

使用全局变量声明:

var power = document.getElementById("power").value;
function getSelectValuePower() {
    sessionStorage.setItem("power", power);
    console.log(power);
}

function getValue() {
    var number = 10;
    var selectedValue = number + power;
    console.log(selectedValue);
}

使用类:

class PowerSelector {
  constructor() {
    this.powerElement = document.getElementById("power");
    this.number = 10;
  }

  getSelectValuePower() {
    const power = this.powerElement.value;
    sessionStorage.setItem("power", power);
    console.log(power);
  }

  getValue() {
    const selectedValue = Number(this.number) + Number(this.powerElement.value);
    console.log(selectedValue);
  }
}

const powerSelector = new PowerSelector();
powerSelector.getSelectValuePower();
powerSelector.getValue();
英文:

You can do it by declaring power outside the function or by creating a class. Here's how you can do it.

Using global declaration:

var power = document.getElementById("power").value;
function getSelectValuePower() {
    sessionStorage.setItem("power", power);
    console.log(power);
}

function getValue() {
    var number = 10;
    var selectedValue = number + power;
    console.log(selectedValue);
}

Using a class:

class PowerSelector {
  constructor() {
    this.powerElement = document.getElementById("power");
    this.number = 10;
  }

  getSelectValuePower() {
    const power = this.powerElement.value;
    sessionStorage.setItem("power", power);
    console.log(power);
  }

  getValue() {
    const selectedValue = Number(this.number) + Number(this.powerElement.value);
    console.log(selectedValue);
  }
}

const powerSelector = new PowerSelector();
powerSelector.getSelectValuePower();
powerSelector.getValue();

huangapple
  • 本文由 发表于 2023年3月7日 22:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定