简体   繁体   English

访问私有函数中的全局(javascript)变量

[英]Accessing global(javascript) variables in a private function

I'm currently working on a calculator for learning purposes and everything works, in a way, but not in the way I'd want it to. 我目前正在研究一个用于学习目的的计算器,一切都在某种程度上有效,但不是我希望它的方式。

Here's the adding up part of the code: 这是代码的累加部分:

HTML: HTML:

<input type="number" id="int1" />
<input type="number" id="int2" />
<input type="button" onClick="sumOf();" />
<div id="result"></div>  // To display the result

JS: JS:

var int1 = document.getElementById("int1").value;
var int1 = document.getElementById("int2").value;
var result = document.getElementById("result");

function sumOf(int1, int2) {
  result.innerHTML = parseFloat(int1) + parseFloat(int2);
}

The way it is right now, if I call the function via the onClick event it will result in NaN. 它现在的方式,如果我通过onClick事件调用该函数,它将导致NaN。 Checking the type of the int's results in 'undefined'. 检查int的类型结果为'undefined'。 So I put the variables inside the function and it works fine because now the function can reach them. 所以我把变量放在函数中,它工作正常,因为现在函数可以到达它们。

My question is how can I give access to the variables outside of the function? 我的问题是如何才能访问函数之外的变量? I tried creating an object, but then I had problems referring to the right function inside the object in the onClick event, I tried "objectName.functionInsideTheObject();". 我尝试创建一个对象,但是在onClick事件中引用对象内部的正确函数时遇到了问题,我尝试了“objectName.functionInsideTheObject();”。 The result was it didn't work. 结果是没有用。

How can I make this work? 我怎样才能做到这一点? New functions will be added for other calculations and it's tedious to copy paste the variables all the time. 将为其他计算添加新函数,并且始终复制粘贴变量非常繁琐。

EDIT: 编辑:

My problem has something to do with ".value" in the variable declaration. 我的问题与变量声明中的“.value”有关。 If I declare a variable as such: 如果我声明一个变量:

var int1 = document.getElementById("int1");  // Not getting the value directly
var int2 = document.getElementById("int2");

And then reference to the value of the variable inside the function like this: 然后引用函数内部变量的值,如下所示:

function sumOf() {
  result.innerHTML = parseFloat(int1.value) + parseFloat(int2.value);
}

It works fine. 它工作正常。 I don't know why .value doesn't work in global scope. 我不知道为什么.value在全局范围内不起作用。 Still not perfect, but less tedious. 仍然不完美,但不那么乏味。

I believe that if you create it like this: 我相信如果你这样创建它:

int1 = document.getElementById("int1").value;

without the var it's then a global. 如果没有var它就是全局的。

The way you declared the variables is Right. 声明变量的方式是正确的。

And these variables are NaN as you are agaian padding them as Arguments in your function 并且这些变量是NaN,因为你将它们填充为函数中的Arguments

function sumOf(int1, int2) {
  result.innerHTML = parseFloat(int1) + parseFloat(int2);
}

you should call the function like 你应该调用这个函数

function sumOf() {
  result.innerHTML = parseFloat(int1) + parseFloat(int2);
}

But I think will be better if you declare your function like 但是我觉得如果宣布你的功能会更好

function sumOf() {
  var int1 = document.getElementById("int1").value;
  var int1 = document.getElementById("int2").value;
  var result = document.getElementById("result");

  result.innerHTML = parseFloat(int1) + parseFloat(int2);
}

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

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