调用原型中列出的函数

Calling a function listed inside a prototype

本文关键字:函数 原型 调用      更新时间:2023-09-26
CircularCountDown.prototype = {
    init: function () {
        this.formatData();
        this.draw();
        this.start();
    },
    start: function () {
        if (typeof this.data.beforeStart == "function") {
            this.data.beforeStart(this);
        }
        this.show();
        this.starDecrementTimeEvent();
        var time = this.getFormattedTimeByCircle();
        this.animate(time);
    },
    starDecrementTimeEvent: function () {
        var that = this;
        console.log(that);
        this.decrementTimeEvent = setInterval(function () {
            that.time -= 1;
            that.setTime(that.getStringTime(that.time));
            if (that.time <= 0) {
                that.time = 0;
                that.stopDecrementTimeEvent();
                if (typeof that.data.end == "function") {
                    that.data.end(that);
                }
            }
        }, 1000);
    },
    stopDecrementTimeEvent: function () {
        clearInterval(this.decrementTimeEvent);
    }
}

我正在使用一个循环计时器的插件,我希望调用stopDecrementTimeEvent函数来在另一个JS文件中停止计时器。我不熟悉如何用这种格式来称呼它。

任何指导都很好,谢谢。

我正在使用一个循环计时器的插件,我希望调用stopDecrementTimeEvent函数来在另一个JS文件中停止计时器。我不熟悉如何将其称为这种格式的

您将有一个通过new CircularCountDown创建的实例,例如:

var theInstance = new CircularCountDown(/*...args here if needed...*/);

创建的实例(对象(将具有CircularCountDown.prototype称为其原型的对象,这意味着它继承了其所有属性。

因此,您只需在该实例上调用stopDecrementTimeEvent

theInstance.stopDecrementTimeEvent();

这将在原型上找到属性stopDecrementTimeEvent,然后用引用实例(对象(的this调用它,该实例的decrementTimeEvent属性中有定时器句柄,准备由stopDecrementTimeEvent使用。