gpt4 book ai didi

Javascript/Jquery 3.3 从非严格到严格和顺序调用的转换函数

转载 作者:行者123 更新时间:2023-12-01 01:14:21 25 4
gpt4 key购买 nike

我正在尝试将旧的非严格函数转换为与严格版本和 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();return _modale.pre(...).pre 不是函数

如何根据严格指令更改功能并获得相同的操作?

最佳答案

您需要在函数中返回此才能使其可链接:

pre: function(){
this.livello_m--;

return this; // Here
}

关于Javascript/Jquery 3.3 从非严格到严格和顺序调用的转换函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54904059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com