该插件具有此变量
this.onValid = options.onValid || function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); };问题是,当我定义一个新实例时,我想要向this.onValid添加一个新函数,我不想覆盖默认事件?
如何扩展变量this.onValid?
发布于 2011-12-24 16:00:22
this.onValid = options.onValid || function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); };
//That above is already defined;
this._onValid = this.onValid; //Make a backup function
this.onValid = function (){
this._onValid(); //Call the actual function
//Your extended code here //Call your code
}这是特定于您的代码的。为了更好地理解它,这个例子可能会有所帮助:
writeA = function (){
document.write('a');
};
//Now I want to extend the writeA function to write 'b' as well
_writeA = writeA;
writeA = function (){
_writeA(); //So now I call whatever the function originally did
document.write('b'); //And now I execute what I want to add to the function
};Example's demo
我只是试着修复你在评论中提供的代码。试试这个:
var obj1 = {
this._onValid = this.onValid; //Do this assuming this.onValid is already defined
onValid: function(){ //Redefine it
this._onValid();
newFunction();
}
};编辑
来自插件的。只需将其编辑为如下所示:
this.onValid = options.onValid || function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); };
this._onValid = this.onValid;
this.onValid = function (){
this._onValid();
//YOUR CODE HERE
};我已经编辑了原始文件,使其看起来像那个here.
哈,我刚刚意识到我在这里放了和开始时一样的代码。好吧,那是因为这就是它应该看起来的样子。查看我编辑过的文件,搜索//YOUR CODE HERE并在其中添加代码。
https://stackoverflow.com/questions/8623435
复制相似问题