我下面有一个简单的类,该类开始,然后每秒更新一次计数。我将如何为其添加功能以侦听特定值,然后触发回调?

function Counter() {
    this.currentCount = 0;
}

Counter.prototype.start = function() {
    setInterval(this.update, 1000);
};

Counter.prototype.when = function(value, callback) {
    callback(value);
};

Counter.prototype.update = function() {
    this.currentCount++;
};


在我看来,它会像这样工作。

var counter = new Counter();
counter.when(50, function(value) {
    console.log('We arrived at ' + value + ', the requested value.');
});
counter.start();

最佳答案

这只是一个不错的作业,我将为您完成;)请看一下我的解决方案:

function Counter() {
    this.currentCount = 0;
    this.conditions = [];
    this.interval = undefined;
}

Counter.prototype.start = function() {
    if (!this.interval) {
        var that = this;
        this.interval = setInterval(function () {
            that.update();
        }, 1000);
    }
};

Counter.prototype.stop = function () {
    if (this.interval) {
        clearInterval(this.interval);
        this.interval = undefined;
    }
    this.currentCount = 0;
};

Counter.prototype.when = function(value, callback) {
    var that = this;
    this.conditions.push(function () {
        if (that.currentCount === value) {
            callback.call(that, value);
        }
    });
};

Counter.prototype.update = function() {
    this.currentCount++;
    for (var i = 0, l = this.conditions.length; i < l; i++) {
        var condition = this.conditions[i];
        condition();
    }
};

var counter = new Counter();
counter.when(50, function(value) {
    console.log('We arrived at ' + value + ', the requested value.');
});
counter.when(60, function (value) {
    console.log('Stop at ' + value + '!');
    this.stop();
});
counter.start();


fiddled

在隐藏私有变量时,这里的另一个答案是一个很好的论据,但实现起来有点太混乱了,所以这是另一种类似的方法。代替共享的原型函数,它使用实例函数。有人可能会说这需要更多的内存,但是我不认为这很重要,并且可以在实际的构造函数中轻松拥有私有变量。

var Counter = function () {
    var that = this, currentCount = 0,
        conditions = [], interval;
    var update = function () {
        currentCount++;
        for (var i = 0, l = conditions.length; i < l; i++) {
            var condition = conditions[i];
            condition();
        }
    };
    this.start = function () {
        if (!interval) {
            interval = setInterval(function() {
                update.call(that);
            }, 1000);
        }
    };
    this.when = function (value, callback) {
        conditions.push(function () {
            if (currentCount === value) {
                callback.call(that, value);
            }
        });
    };
    this.stop = function () {
        if (interval) {
            clearInterval(interval);
            interval = undefined;
        }
        currentCount = 0;
    };
};

var counter = new Counter();
counter.when(50, function(value) {
    console.log('We arrived at ' + value + ', the requested value.');
});
counter.when(60, function (value) {
    console.log('Stop at ' + value + '!');
    this.stop();
});
counter.start();


看到它fiddled

还要注意,在两个示例中,counterinstanceof CounterObject
Counterinstanceof FunctionObject(为什么我喜欢写那么多代码;)

09-30 16:38
查看更多