访问函数外部的JavaScript函数变量

时间:2021-10-01 15:56:02

I apoligize for my newbness but I'm not clearly understanding how to access variables within a function outside of the function. I understand that you need to pass them some how but I'm not 100% sure on the whys and hows.

我为自己的新事道尽职尽责,但我并不清楚如何在函数之外的函数中访问变量。我知道你需要通过一些方法,但我不能百分之百地确定他们的原因和方法。

Taking the below code for example, I want to use the var degree throughout the code outside of the function. How do I do it?

以下面的代码为例,我想在函数外部的代码中使用var度。我该怎么做?

function DegreeToMil() 
{

//Degree's to Mils: 1 Degree = 17.777778 Mils

var degree = 10;
var mils = degree * 17.777778;

return mils;

 }

2 个解决方案

#1


4  

It is actually fairly simple, just define it outside of the function.

它实际上相当简单,只需在函数外部定义它。

Edit: Updated with an example, and comments explaining what was done and how it works.

编辑:更新了示例,并说明了解完成的内容及其工作原理。

    DegreesToMils.degrees = 10; /* This is a static variable declaration to make sure it isn't undefined
                             * See note 1 below */

function DegreesToMils(degrees) {
    if (degrees !== undefined) {
        DegreesToMils.degrees = degrees; /* If the parameter is defined, 
                                          * it will update the static variable */
    }

    var milsPerDegree = 17.777778; /* This is a variable created and accessible within the function */

    return DegreesToMils.degrees * milsPerDegree; /* The function will return 177.77778 */
}

console.log(DegreesToMils.degrees); /* Prints 10, Note 1: This would be undefined if
                                     * not declared before the first call to DegreesToMils() with a 
                                     * defined parameter
                                     */
console.log(DegreesToMils(10)); /* Prints 177.77778 */
console.log(DegreesToMils(9)); /* Prints 160.00000200000002, Sets DegreesToMils.degrees to 9 */
console.log(DegreesToMils.degrees); /* Prints 9 */

#2


1  

function DegreeToMil() 
{

    //Degree's to Mils: 1 Degree = 17.777778 Mils

    var degree = 10;
    var mils = degree * 17.777778;
    var result = [degree, mils]; // it's an array
    return result;
}

// use it like this

var myResult = DegreeToMil();
console.log(myResult[0]); // degree
console.log(myResult[1]); // mils

#1


4  

It is actually fairly simple, just define it outside of the function.

它实际上相当简单,只需在函数外部定义它。

Edit: Updated with an example, and comments explaining what was done and how it works.

编辑:更新了示例,并说明了解完成的内容及其工作原理。

    DegreesToMils.degrees = 10; /* This is a static variable declaration to make sure it isn't undefined
                             * See note 1 below */

function DegreesToMils(degrees) {
    if (degrees !== undefined) {
        DegreesToMils.degrees = degrees; /* If the parameter is defined, 
                                          * it will update the static variable */
    }

    var milsPerDegree = 17.777778; /* This is a variable created and accessible within the function */

    return DegreesToMils.degrees * milsPerDegree; /* The function will return 177.77778 */
}

console.log(DegreesToMils.degrees); /* Prints 10, Note 1: This would be undefined if
                                     * not declared before the first call to DegreesToMils() with a 
                                     * defined parameter
                                     */
console.log(DegreesToMils(10)); /* Prints 177.77778 */
console.log(DegreesToMils(9)); /* Prints 160.00000200000002, Sets DegreesToMils.degrees to 9 */
console.log(DegreesToMils.degrees); /* Prints 9 */

#2


1  

function DegreeToMil() 
{

    //Degree's to Mils: 1 Degree = 17.777778 Mils

    var degree = 10;
    var mils = degree * 17.777778;
    var result = [degree, mils]; // it's an array
    return result;
}

// use it like this

var myResult = DegreeToMil();
console.log(myResult[0]); // degree
console.log(myResult[1]); // mils