Функция преобразования Javascript / Jquery 3.3 из нестрогих в строгие и последовательные вызовы - PullRequest
0 голосов
/ 27 февраля 2019

Я пытаюсь превратить старую нестрогую функцию в версию, совместимую со строгой версией и с 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 is not a function

Как я могу изменить функцию в соответствии со строгими директивами и получить ту же операцию?

1 Ответ

0 голосов
/ 27 февраля 2019

Вам нужно return this в вашей функции, чтобы она была цепной:

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

  return this; // Here
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...