简体   繁体   English

如何在不使变量在JavaScript中成为全局变量的情况下使用另一个函数中的变量?

[英]How do I use a variable from another function without making the variable global in JavaScript?

Essentially, I have two different functions performing different calculations based on HTML form inputs. 本质上,我有两个不同的函数,它们基于HTML表单输入执行不同的计算。

I need a 3rd function to take one variable from each of them to perform the last math calculation. 我需要第三个函数从每个变量中获取一个变量以执行最后的数学计算。

Is there any way to do that without making the variables global or combining the functions? 有什么方法可以使变量成为全局变量或不合并函数?

Here are the functions: 功能如下:

function getStateTax(){
    var income = salaryBox.value;
    var sel = document.getElementById("state");
    var state = sel.options[sel.selectedIndex].value;
    var rate = taxRates[state];
    var el = document.getElementById("rate");
    el.innerHTML = rate;
    stateTaxElement.innerHTML = "Your state tax amount is: $" + Math.round(((income * (1 + rate)) - income) * 100) / 100;
}

function getFedTax() {
    var fedTax = 0;
    var income = salaryBox.value;
    if (document.getElementById("single").checked) {
        if (income <= 8350) {
            fedTax = income * .10;
        } else if (income <= 33950) {
            fedTax = 8350 * 0.10 + (income - 8350) * 0.15;
        } else if (income <= 82250) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
        } else if (income <= 171550) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
        } else if (income <= 372950) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;
        } else {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
        }
    }
    if (document.getElementById("jointorwidow").checked) {
        if (income <= 16700) {
            fedTax = income * 0.10;
        } else if (income <= 67900) {
            fedTax = 16700 * 0.10 + (income - 16700) * 0.15;
        } else if (income <= 137050) {
            fedTax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 137050) * 0.25;
        } else if (income <= 208850) {
            fedTax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
        } else if (income <= 372950) {
            fedTax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;
        } else {
            fedTax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35;
        } 
    }
    if (document.getElementById("separate").checked) {
        if (income <= 8350) {
            fedTax = income * 0.10;
        } else if (income <= 33950) {
            fedTax = 8350 * 0.10 + (income - 8350) * 0.15;
        } else if (income <= 68525) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
        } else if (income <= 104425) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
        } else if (income <= 186475) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;
        } else {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;
        }
    }  
    if (document.getElementById("head").checked) {
        if (income <= 11950) {
            fedTax = income * 0.10;
        } else if (income <= 45500) {
            fedTax = 11950 * 0.10 + (income - 11950) * 0.15;
        } else if (income <= 117450) {
            fedTax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
        } else if (income <= 190200) {
            fedTax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
        } else if (income <= 372950) {
            fedTax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;
        } else {
            fedTax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;
        }
    }
    fedTaxElement.innerHTML = "Your federal tax amount is: $" + Math.round(fedTax * 100) / 100;
}

What I need to do is make a third function and add the fedTax and first income variables together and subtract them from a new variable. 我需要做的是制作第三个函数,并将fedTax和第一个收入变量加在一起,然后从新变量中减去它们。

Functions in javascript have the capability of returning values (as they do in most if not all programming languages). javascript中的函数具有返回值的能力(就像大多数(即使不是全部)编程语言一样)。

This means that when a function is finished executing you can return a specific value. 这意味着函数完成执行后,您可以return特定值。

A simple example of returning a result would be a simple add function: 一个简单的返回结果的例子是一个简单的add函数:

var add = function(a, b) {
    return a + b;
}

var result = add(20, 30); // result is 50
console.log(result);

Ofcourse this is a bad example since there's the + operator that allows you to add numbers or concatenate strings but still, it shows what return does. 当然,这是一个不好的例子,因为有+运算符允许您添加数字或连接字符串,但是它仍然显示return作用。

If you'd replace the return a + b; 如果要替换return a + b; with just a + b; 只有a + b; in that line then no value will be returned. 在该行中,将不返回任何值。

Now imagine we have a function that takes 3 parameters, the first two will be added onto each other and the third will be subtracted from the result. 现在假设我们有一个函数,该函数带有3个参数,前两个参数将彼此相加,第三个参数将从结果中减去。

We can use the add method nested within our function to return a result that we can continue our calculations with. 我们可以使用嵌套在函数中的add方法来返回可以继续进行计算的结果。

This function would look like this: 该函数如下所示:

var custom_calc = function(a, b, c) {
    var add_res = add(a, b);
    return (add_res - c);
}

var result = custom_calc(10, 20, 20); // result is 10
console.log(result);

So in your first two functions, you'll want the last line of each function to be a return statement returning the variable that you need in the third function. 因此,在前两个函数中,您希望每个函数的最后一行是一个return语句,该语句返回第三个函数中所需的变量。 In my example we used add to get a result, stored it in a variable and we used that variable to do further calculations and we return the result of that. 在我的示例中,我们使用add获得结果,将其存储在变量中,然后使用该变量进行进一步的计算,然后返回结果。

The same applies to your situation, good luck! 同样适用于您的情况,祝您好运!

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

相关问题 如何在不使用全局变量的情况下从 JavaScript 中的嵌套 For 循环返回变量? - How do I return a variable from nested For Loops in JavaScript without using a global variable? 在jquery中将变量从一个函数专门传递给另一个函数而不使其成为全局变量 - Passing a variable specifically from one function to another in jquery without making it global 如何在 JavaScript 中将函数用作变量? - How do I use a function as a variable in JavaScript? 如何在不将其用作全局变量的情况下在另一个函数中使用一个函数的变量? - How to use one function's variable in another function without it being a global variable? 如何使此javascript函数不使用全局变量? - How can I make this javascript function not use a global variable? html / javascript / php:如何使用“全局变量”? - html/javascript/php: how do i use a “global variable”? 如何在javascript中的函数内使用全局变量? - how to use a global variable inside a function in javascript? 如何在没有全局变量的情况下访问此变量? - How do I access this variable without a global? 如何在 function 中使用全局变量,然后在另一个 function 中使用 output - How to use Global Variable in a function and then use the output of the function in another function 如何在JavaScript中创建全局变量? - How do I make a Global Variable in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM