Javascript Es6 Tagged Templates - When is raw used? When is cooked used?

不问归期 提交于 2019-12-01 06:25:29

The tag function func is passed just one array. That array comes from the _taggedTemplateLiteral function, which takes the incoming "strings" parameter and adds a single property to it.

The function func would be declared like this (ES5-style):

function func(strings) {
  var params = [].slice.call(arguments, 1);
  // do stuff
}

If, inside func, the code needed to use the "raw" strings, it would just access the .raw property of the "strings" variable:

function func(strings) {
  var params = [].slice.call(arguments, 1);

  var raw2 = strings.raw[2];

  // do stuff
}

So the "user" — the author of the tag function — does have control. Code in the tag function is free to examine the original content of the template parts whenever it wants. It's probably the case that tag functions that essentially implement a DSL might want to only use the raw strings, while simpler template mechanisms won't care and will be happy to use the "parsed" strings.

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