1. 22
  1.  

  2. 9

    I think this style is wonderful. To me ‘this’s, especially in Javascript, always felt challenging in the way dynamic scoping is challenging—you have to keep a lot of connections alive in your mind beyond the ones literally written down locally to a piece of code.

    Reading this ‘this’-free code makes me think of lexical scoping. Behaviors may grow complex, but understanding any particular layer requires only looking at the code immediate to it.

    1. 2

      Anyone have experience debugging such code? Obviously, the object inspector is not of much help (or can they inspect closures now?)

      1. 2

        One problem with “this” approach (pun intended) is the memory overhead. Factory patterns, as shown here, return new functions every call, meaning higher memory usage. If you use prototypes instead (and, without being snarky, learn how scope works in JavaScript), you don’t get this additional memory usage as each instance of a type gets it’s methods from it’s prototype. Therefore all instance of a type share a reference to a single object (the prototype). It should be obvious why this is preferable in the general case.

        Of course there are some cases where this approach is more appropriate e.g. mixins

        1. 1

          A small, but powerful cognitive trick we use is to start each file with : var context = this;

          This simple change does wonders when reasoning about the meaning of ‘this.’ I know the idea of ‘this’ comes from Java and other OO languages, but maybe all the trouble simply stems from bad naming of this important variable.