0

我有下一个代码:

var Test = (function() {
    var counter = 0;
    var increaseCounter = function() {
        this.counter += 1;
    }
    increaseCounterPrivate = function() {
        // does not affect this.counter! //
        increaseCounter();
    }
    var counterLogic = function() {
        // ... do something here ... //
        increaseCounterPrivate();
    }
    return {
        counter: counter,
        increaseCounter: increaseCounter,
        counterLogic: counterLogic
    };
});

var test = new Test();
test.increaseCounter();
console.log(this.counter); //1
test.counterLogic();
console.log(this.counter); //1 again, should be 2

上面的代码没有意义(它只是为了测试),但我想知道一般我是否可以调用公共方法,它会调用私有方法,最终会改变公共变量。如何重写以显示正确的值?

4

0 回答 0