【发布时间】:2019-02-27 11:06:45
【问题描述】:
我正在尝试将旧的非严格函数转换为与严格版本和 Jquery 3.3 兼容的版本
旧功能让我也可以按顺序调用各种功能,得到一个最终结果,在新功能中我多次尝试后无法重现。
旧函数是:
var num_modali = 0;
_modale = function(){
this.livello_m = num_modali;
this.percorso = function(){
return (this.livello_m > 0) ? $('body').find('#modale_' + this.livello_m).find(modale_content) : $('body');
};
this.listarecord = function(){
return lista_record = (this.livello_m > 0) ? '#lista_records_modale' : '#lista_records';
};
this._pre = function(){
this.livello_m--;
return this;
};
this._go = function(){
return this.percorso();
};
this._getlivello = function(){
var livello = (this.livello_m > 0) ? this.livello_m : 0;
return livello;
};
this._chiudi = function(where){
$destroy_modale();
return this;
};
this._delete = function(what){
this.percorso().find(this.listarecord()).find(what).remove(what);
return this;
};
if(this instanceof _modale){
return this;
}else{
return new _modale();
}
};
我也可以这样调用:_modale()._pre()._pre()._go();
全局变量 num_modali 被用于管理模态的第二个函数使用
新功能:
var _modale = {
livello_m: num_modali,
percorso: function(){
return (this.livello_m > 0) ? 'body #modale_' + this.livello_m + ' .modale_content' : 'body';
},
listaRecord: function(){
return (num_modali > 0) ? '#lista_records_modale' : '#lista_records';
},
pre: function(){
return this.livello_m - 1;
},
go: function(){
return this.percorso();
},
getlivello: function(){
return (this.livello_m > 0) ? this.livello_m : 0;
},
chiudi: function(){
modale.destroyModale();
//return this;
},
_delete: function(what){
_modale.percorso().find(_modale.listaRecord()).find(what).remove(what);
}
};
如果我尝试执行相同的顺序调用:_modale.pre().pre().go();
返回_modale.pre(...).pre is not a function
如何根据严格的指令改变功能并获得相同的操作?
【问题讨论】:
-
所有想要链接的函数都需要返回一个
_modale的实例。 (这与strict或jQuery无关)
标签: javascript jquery sequential strict