Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It doesn't just support minification, it can also be used in closures. When you're creating functions, for example to use in a callback, you have to take care of what "this" is when the function is called. A common mistake when starting out in Javascript is to get the wrong 'this'. The "var me = this" pattern helps to avoid that. For example</p> <pre><code>function doSomething() { var me = this; callSomethingAsynchronous(42, function (result) { // Here 'this' will depend on the caller of the callback functon. me.theResult = result; }); } </code></pre> <p>Note that frameworks like Ext discourage this and have you pass the scope variable along with the callback. </p> <pre><code>callSomethingAsynchronous(42, me.handlerFunction, me); </code></pre> <p>It allows you do leave the definition of the handlerFunction on the prototype rather than keep creating them. Sometimes a closure is just the better way because you have to enclose information that is only known at runtime</p> <pre><code>function sayHelloInAContrivedWay (name) { getTheGreetingAsync(function (greeting) { console.log(greeting + name); }); } </code></pre> <p>I use "var me = this" whenever I'm accessing this more than once in a function. The minifier does reduce it to a single character. If I'm accessing a child object I'll create a var for that too</p> <pre><code>function keepInnerWombatFit () { var me = this, wombat = me.inner.wombat; wombat.run(); wombat.jump(); wombat.sitDown(); me.excerciseCount++; } </code></pre> <p>I've seen code with huge nested accesses typed out (or cut and pasted) again and again and again, which cannot be minified, may cost a little performance and is harder to read and maintain.</p> <pre><code>this.inner.wombat.run(); this.inner.wombat.jump(); this.inner.wombat.sitDown(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload