Javascript: always execute function in execution context

自古美人都是妖i 提交于 2019-12-18 09:45:55

问题


I wrote this fast-templating function:

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);
}

Which does this:

var foo = "bar", bar = "foo";
templatize("We are {{foo}} and {{bar}}, but not {{crazy}}"); // "We are bar and foo but not {{crazy}}"

I'm quite happy with this except that I have scoping problem. For sure, the templatize method will be accessible through namedscope, but then, the current context of execution of templatize is not accessible in my function automatically.

Something like calling $.proxy(templatize, this)("We are {{foo}} and {{bar}}, but not {{crazy}}") should work, right?

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.

I'm struggling with .call(), .apply(), and other closures, but I think I read somewhere over the internet that it was possible. Thanks


回答1:


why don't you pass an object containing the view variables? would be cleaner then potentially displaying any existing variable in your view.

var templatize = function(string, variables) {
  return function (string) {
    return string.replace(/{{(.*?)}}/g, function(pattern, match) {
      value = variables[match];
      if (value) {
        return value;
      } else {
        return pattern;
      }
    });
  }.call(this, string);
}



回答2:


You can avoid using jQuery doing this :

var templatize = function(string) {
    var me = this; // the data source
    return string.replace(/{{(.*?)}}/g, function (full, key) {
        // "this" refers to the string itself
        return me[key] || full;
    });
}

In case you want to use jQuery.proxy(), wrap the replacement function :

var templatize = function(string) {
    return string.replace(/{{(.*?)}}/g, jQuery.proxy(function (full, key) {
        // "this" now refers permanently to the data source
        return this[key] || full;
    }, this));
}

In both cases you can bind the data source to this using call :

templatize.call({ hello: 'Hi!' }, '{{hello}}');

Going further

You could optimize by compiling the template for reuse :

function compile(tpl) {
    var i = -1, tmp = [];
    tpl = tpl.split(/{{([^{}]+)}}/);
    while (++i < tpl.length) {
        if (i % 2) tmp.push('this["' + tpl[i] + '"]');
        else if (tpl[i]) tmp.push('"' + tpl[i].replace(/"/g, '\\"') + '"');
    }
    return new Function(
        'return [' + tmp.join() + '].join("");'
    );
}

Usage example :

var tpl = compile('{{hello}} {{hello}}');
tpl.call({ hello: 'Hi!' }); // "Hi! Hi!"
tpl.call({ hello: 'Yo!' }); // "Yo! Yo!"

Regarding the example above, here is the function returned by compile :

function () {
    return [this["hello"]," ",this["hello"]].join("");
}

Note that you can use an array as well :

var tpl = compile('{{1}} {{0}}');
tpl.call(['a', 'b']); // "b a"

Performance test : http://jsperf.com/template-compiling.



来源:https://stackoverflow.com/questions/20885411/javascript-always-execute-function-in-execution-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!