There is such code:

loadContent = { limitAjax: 0, start: function (btn) { console.log(this.limitAjax); // not equal 4 // Ajax response this.limitAjax = response.limit; // response.limit = 4 } } 

Ajax returns the new limit value that is assigned to this.limitAjax . But the next time the function is called, the value of this.limitAjax is still = 0.

Do not want to make a superglobal variable ...

    2 answers 2

    If you check your piece of code without Ajax, then it will absolutely work fine:

     loadContent = { limitAjax: 0, start: function (btn) { this.limitAjax = 4; } } loadContent.start(); console.log(loadContent.limitAjax); //Вывод: 4 

    Therefore, the question is how you use this Ajax there. Either you do not take into account that Ajax is asynchronous, or, most likely, incorrectly use scopes when returning the result. But since you decided not to give this very important piece of code, it’s impossible to point out a specific error.

      @Jony , in general I would like to see how you call a function (and what this is in it), but in general, the js approach to static variables implies a closure:

       start: (function() { var limit = 0; return function () { // тело этой функции будет видеть limit, а весь остальной мир - нет }; })(),