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