The Javascript handler reference for the environment was defined in

Is there a way for the javascript method to refer to the environment in which it was defined, as opposed to how it was called? In this particular case, the method is defined in the object literal.

t

var obj = {
            init: function(){
                  jQuery("form").on('keyup','input',{self:this},this.checkDetails);
            }
            checkDetails: function(event){

                  console.log(this); 
                  // 'this' references the input object which invoked the method

                 console.log(event.data.self); 
                  //provides a correct reference to the object 'obj', but I had to pass it
            }

     }

thismust refer to the shared object in which it was defined.

+4
source share

Source: https://habr.com/ru/post/1547557/


All Articles