Is it possible and how to implement this?

var obj = (function ($) { return { func1: function () { console.log("do something"); }, func2: function () { // do something $("#debug").click(function () { // do something func1(); // do something }) } } })(jQuery); obj.func2(); // ΠΏΠΎ ΠΊΠ»ΠΈΠΊΡƒ Π½Π° #debug ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ - Uncaught ReferenceError: func1 is not defined 

    1 answer 1

    With minimal changes to your code here:

     var obj = (function ($) { return { func1: function() { console.log("do something"); }, func2: function () { $("#debug").click(function () { this.func1(); //2. обращаСмся ΠΊ Π½ΡƒΠΆΠ½ΠΎΠΌΡƒ контСксту }.bind(this)) //1. привязываСм контСкст } } })(jQuery); obj.func2(); 

    Meaning next
    one). The handler is passed the context of the object. You can do this not via bind if you do not want to replace the entire context of the handler, for example, by saving it in a local variable in the spirit of var me = this
    2). Actually, we have a context, it has a function, it remains to invoke it.

    Yes, I would add that it would be better to define the functions that you return to return, it will be much clearer. But this is a stylistic question.