function Cl(){; this.view=function(){ input=document.createElement('input');'inputs'; input.addEventListener('click',function(){console.log(}) document.body.appendChild(input); } } obj=new Cl; obj.view(); 

Help call console.log ( to get 100; You need to refer to the object created by the constructor. Consider all the options. The more the better. Thanks.

    2 answers 2

    or so:

     input.addEventListener('click',function(){ console.log( }.bind(this)) 

    or so:

     function Cl(){ var self = this;; this.view=function(){ ... input.addEventListener('click',function(){console.log(}) ... } } 
    • Grateful. Who will read -… - zloctb
    • one
      it is generally canonically bind here:! function () {var _slice = Array.prototype.slice Function.prototype.bind = function (context) {var args = (arguments, 1), func = this; return function () {return func.apply (context, args.concat ( (arguments, 0))); }}} () - Specter
    • It is possible without .bind(this) you already pass the context when you hang the handler on the element. - lampa
    • in general, the context is determined during a function call, not its definition, or I misunderstood you - Specter
    • one
      that's it, the vehicle wanted the context not to be the element on which the handler was hung, but obj - Specter
     input.addEventListener('click',function(t){return function(){console.log(}}(this)) 
    • Grateful - zloctb