Plain references to functions lose context

 Taking a reference to a function of a class object sometimes loses the context.


If I say:


      var g = obj.f;

      g();


Then the 'this' for 'g' is 'window' not 'obj'.


To get round this, sometime I say:


    g.call(obj);


With a timeout, sometimes I say:


          var that = this;


          function callg()

          {

              that.g();

          }


          setTimeout(callg, 1000);


I have been converting 'modules' to objects made with 'prototypes' and 'new'. By a 'module' I mean a self-executing anonymous function (a closure) returning references to inner functions.


There are more tips at: bbingo.xyz/techtips/

Comments

Popular posts from this blog

webkit-tap-highlight-color in CSS

Steve Jobs quotes