M
M
Maxim Zolotoy2018-02-15 12:07:23
JavaScript
Maxim Zolotoy, 2018-02-15 12:07:23

How to access via this to other methods?

Here I have the following code
1) DialogSheet - the constructor function declared earlier. And I wrote down a method for her through the prototype.
2) I added other methods to the prototype of this function. For example the preloaderHide method
The problem is that preloaderHide is inside another function. And so this no longer refers to the constructor function and I get an error.
How can you solve this problem? It only occurred to me to declare some kind of variable at the beginning of the method and assign this to it and then refer to it through it - but maybe there is some more correct solution?

DialogSheet.prototype.load = function() {
  this.preloaderShow();
  $.post("date.php", {
    dialog_sheet: true
  }, function(data) {
    this.preloaderHide(function() {
      if (data) {
        try {
          var result = JSON.parse(data);
          for (var i = 0; i < result.length; i++) {
            var id = result[i]['id'];
            var name = result[i]['name'];
            var img = result[i]['avatar'];
            var allMsg = result[i]['all_messages'];
            var newMsg = result[i]['new_messages'];
            var status = result[i]['online'];
            var dialog = new Dialog(id, name, img, allMsg, newMsg, status);
            dialog.render();
          }
        } catch (err) {
          console.error('ошибка данных');
          console.log(err);
        }
      } else {
        this.emptyTitleRender();
      }
    });
  });
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2018-02-15
@spacenear

1. Variable option
2. bind
3. arrow function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question