简体   繁体   English

变量范围和共享信息没有全局变量

[英]Variable scope and sharing information without global variables

If I have: 如果我有:

function firstFunction(){
    var counter = 0;
    secondFunction();
    secondFunction();
    secondFunction();
    secondFunction();
}
function secondFunction(){
    counter++;
}

I get an error because of the local scope of the variable, but how else am I to do something like this without using global variables? 由于变量的局部范围,我得到一个错误,但是如果不使用全局变量,我怎么做这样的事情呢?

One way is to use a closure: 一种方法是使用闭包:

(function() {
    var counter = 0;

    function firstFunction() {
        secondFunction();
        secondFunction();
        secondFunction();
        secondFunction();
    }

    function secondFunction() {
        counter++;
    }
})();

Alternately, you could pass in the value of counter to secondFunction , like this: 或者,您可以将counter的值传递给secondFunction ,如下所示:

function firstFunction() {
    var counter = 0;
    counter = secondFunction(counter);
    counter = secondFunction(counter);
    counter = secondFunction(counter);
    counter = secondFunction(counter);
}

function secondFunction(counter) {
    return ++counter;
}

Use Object to pass counter by reference 使用Object通过引用传递计数器

   function firstFunction() {

        var myObj = new Object();
        myObj.counter = 0;

        secondFunction(myObj);
        secondFunction(myObj);
        secondFunction(myObj);
        secondFunction(myObj);

    }

    function secondFunction(obj) {
        obj.counter++;
    }

Since primitive types (such as integers and strings) in javascript are passed by value, but objects are passed by reference, you need to use an object: 由于javascript中的原始类型(如整数和字符串)是按值传递的,但是对象是通过引用传递的,因此您需要使用一个对象:

// passing primitive type
var counter = 0;
for (var i = 0; i < 4; ++i) { 
    secondFunction(counter);
}
alert(counter); // 0
function secondFunction(counter) {
    counter++;
}

// passing object
var obj = { counter: 0 }
for (var i = 0; i < 4; ++i) { 
    secondFunction(obj);
}
alert(obj.counter); // 4
function secondFunction(obj) {
    obj.counter++;
}

http://jsfiddle.net/MpVqQ/2/ http://jsfiddle.net/MpVqQ/2/

This way you can achieve what you want without using global variables. 这样您就可以在不使用全局变量的情况下实现所需。

As an addition to Elliot Bonneville's answer, you can also do this: 作为Elliot Bonneville答案的补充,你也可以这样做:

var firstFunction = (function()
{
    var counter = 0,
    secondFunction = function
    {
        counter++;
    };
    return function()
    {
        counter = 0;//initialize counter to 0
        secondFunction();
        return counter;
    };
};
console.log(firstFunction());//logs 1, because counter was set to 1 and returned

This is all getting a bit much, but google "JavaScript module pattern" and look into it. 这一切都变得有点多,但谷歌“JavaScript模块模式”并调查它。 You'll soon find out what makes this code tick: 你很快就会发现是什么让这个代码打勾:

var counterModule = (function()
{
    var counter = 0,
    secondFunction = function
    {
        counter++;
    },
    firstFunction = function()
    {
        counter = 0;//initialize counter to 0
        secondFunction();
    },
    getCounter = function()
    {
        return counter;
    }
    setCounter = function(val)
    {
        counter = +(val);
    };
    return {firstFunction: firstFunction,
            getCounter: getCounter,
            setCounter: setCounter};
};
console.log(counterModule.firstFunction());//undefined
console.log(counterModule.getCounter());//1
console.log(counterModule.secondFunction());//ERROR

It's all about exposure of certain closure variables... Keep working at it, this is an important pattern to understand, I promise! 这完全是关于某些闭包变量的曝光......继续努力,这是一个重要的理解模式,我保证!

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

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