【问题标题】:Javascript/Jquery 3.3 conversion function from non strict to strict and sequential callsJavascript/Jquery 3.3 从非严格到严格和顺序调用的转换函数
【发布时间】: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


【解决方案1】:

您需要在您的函数中 return this 才能使其可链接:

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

  return this; // Here
}

【讨论】:

  • 完美,非常感谢,我已经尝试过了,但显然我做错了什么
猜你喜欢
  • 2023-03-24
  • 2011-03-09
  • 2012-11-12
  • 1970-01-01
  • 2014-01-02
  • 2011-08-26
  • 2013-02-14
  • 1970-01-01
  • 2012-06-18
相关资源
最近更新 更多