修改 JQuery 对象

Modify JQuery Object

本文关键字:对象 JQuery 修改      更新时间:2023-09-26

我有一个 JQuery 插件,每次调用插件的主函数时都会创建一个新对象:

函数调用:

new BpNotification( options );

函数本身:

function BpNotification( options ) { 
    this.init();
}
BpNotification.prototype = {
    init: function() {
        this.t = setTimeout(function(){}, 5000);
    }
}

创建对象后,是否可以从"外部"修改此超时选项"t"?

您可以

根据需要更改t

function BpNotification( options ) { 
    this.init();
}
BpNotification.prototype = {
    init: function() {
       this.t = setTimeout(function(){alert('default');}, 500);
    }
}
var Bpn = new BpNotification();
clearTimeout(Bpn.t);
Bpn.t = setTimeout(function(){alert('updated!');}, 500);

演示

您可能希望在原型对象中创建一个 setter 函数来更改 setTimeout 值:

BpNotification.prototype = {
    init: function() {
    },
    updateTimeout: function(newVal){
        this.t = setTimeout(function(),newVal);
    }
};
var bpNot = new BpNotification();
bpNot.init();
bpNot.updateTimeout(10000);