title: Javascript: always execute function in execution context
body: <p>I wrote this fast-templating function:</p>
  
  <pre><code>var templatize = function(string) {
      return function (string) {
        return string.replace(/{{(.*?)}}/g, function(pattern, match) {
          value = this[match];
          if (value) {
            return value;
          } else {
            return pattern;
          }
        });
      }.call(this, string);
  }
  </code></pre>
  
  <p>Which does this:</p>
  
  <pre><code>var foo = "bar", bar = "foo";
  templatize("We are {{foo}} and {{bar}}, but not {{crazy}}"); // "We are bar and foo but not {{crazy}}"
  </code></pre>
  
  <p>I'm quite happy with this except that I have scoping problem. For sure, the <code>templatize</code> method will be accessible through namedscope, but then, the current context of execution of <code>templatize</code> is not accessible in my function automatically.</p>
  
  <p>Something like calling <code>$.proxy(templatize, this)("We are {{foo}} and {{bar}}, but not {{crazy}}")</code> should work, right?</p>
  
  <p>But I'd like to achieve this without needing to call $.proxy() (and without any jQuery preferably) so that context is automatically transfered to the execution one.</p>
  
  <p>I'm struggling with <code>.call()</code>, <code>.apply()</code>, and other closures, but I think I read somewhere over the internet that it was possible. Thanks</p>