1

So I've got something like this...

(function(window) {

    function MyPro(el) {        
        this._init(el);
    }

    MyPro.prototype._init = function(el) {
        var self = this;
        ...
    }

    MyPro.prototype.toggle = function(display) {
        var self = this;
        ...
    }

    MyPro.prototype.clearSelected = function() {
        var self = this;
        ...
    }

    window.MyPro = MyPro;

})(window);

Where many of these methods have callbacks and nesting, and all require the line var self = this;. I can stick with it, if I must, but is there no way that I can do this globally (within the prototype) just once?

I tried...

(function(window) {

    var self = this;        

    function MyPro(el) {        
        self._init(el);
    }

    ...

And then leaving out each individual definition, but it didn't work out. It's been a while since I've gotten heavy into the nuances of JS, and some things like this keep escaping me.

Birrel
  • 4,754
  • 6
  • 38
  • 74
  • No, you cannot. But you could simply use a different approach (`bind`, arrow functions…) than `self` variables to get access to the right object in your callbacks. – Bergi Oct 02 '17 at 10:55

0 Answers0