访问javascript闭包中的变量 [英] accessing variables in javascript closures

查看:100
本文介绍了访问javascript闭包中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var add =(function(){
var counter = 0;
return function(){
var reset = function b $ b counter = 0;
}
return counter + = 1;
}
})();

这是一个自调用函数,创建一个私有变量。如何创建一个函数 reset ,将计数器重置为0?我试着声明的闭包内的函数,但是当我使用add.reset()调用它,它告诉我这个方法是未定义的。



任何帮助在理解是赞赏!

解决方案

如果你想明确地将计数器声明在闭包内,你需要声明复位(即使你不给它一个值)。要使用你的代码,它看起来像这样:

  var reset; 
var add =(function(){
var counter = 0;
return function(){
reset = function(){
counter = 0;
}
return counter + = 1;
}
})();

现在,重置超出了add函数的范围,因此它保留了赋值的值!



为了公平起见,没有理由每次都可以分配重置...可能最好做一些事情:

  var reset; 
var add =(function(){
var counter = 0;
reset = function(){
counter = 0;
}
return function (){
return counter + = 1;
}
})();

或更好的是,如果你想要 add.reset c $ c> to work:

  var counter = function(){
var counter = 0;
this.reset = function(){
counter = 0;
}
this.add = function(){
return counter + = 1;
}
};
var add = new counter();

然后添加是一个完整的对象,或多或少听起来像你想要的。

或者如果你想坚持自我调用的函数:

  var add = (function(){
var counter = 0;
return function(){
this.reset = function(){
counter = 0;
}
return counter + = 1;
}
})();

可能工作。这将是一个从我所看到的一个略微不寻常的范例...


var add = (function () {
    var counter = 0;
    return function () { 
        var reset = function() {
            counter = 0;
        }
        return counter += 1;
    }
})();

This is a self-invoking function that creates a "private" variable. How would I create a function reset that will reset the counter to 0? I've tried declaring the function inside the closure, but when I call it using add.reset(), it tells me this method is undefined.

Any help in understanding is appreciated!

解决方案

If you want to explicitly keep the counter declared inside the closure, you need to declare reset (even if you don't give it a value) outside the closure. To use your code, it would look like this:

var reset;
var add = (function () {
    var counter = 0;
    return function () { 
        reset = function() {
            counter = 0;
        }
        return counter += 1;
    }
})();

Now reset is outside the scope of the add function, so it keeps the value assigned within it!

To be fair, though, there's no reason to assign reset every time you can the result of add... It might be better to do something like:

var reset;
var add = (function () {
    var counter = 0;
    reset = function() {
        counter = 0;
    }
    return function () { 
        return counter += 1;
    }
})();

Or better still, if you want add.reset() to work:

var counter = function () {
    var counter = 0;
    this.reset = function() {
        counter = 0;
    }
    this.add = function () { 
        return counter += 1;
    }
};
var add = new counter();

Then add is a full object, which more or less sounds like what you want.

Or if you want to stick with the self invoking function:

var add = (function () {
    var counter = 0;
    return function () { 
        this.reset = function() {
            counter = 0;
        }
        return counter += 1;
    }
})();

Would probably work. It would be a slightly unusual paradigm from what I've seen though...

这篇关于访问javascript闭包中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆