Javascript Es6标签模板 - 什么时候使用?什么时候煮熟? [英] Javascript Es6 Tagged Templates - When is raw used? When is cooked used?

查看:159
本文介绍了Javascript Es6标签模板 - 什么时候使用?什么时候煮熟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var yo = func` $ {x} + $ {y} \\ \\ n = $ {x + y}`; 






  @ public-node〜/ es6 $ 6to5 tag.js 
use strict;

var _taggedTemplateLiteral = function(strings,raw){
return Object.freeze(Object.defineProperties(strings,{
raw:{
value:Object.freeze (raw)
}
}));
};

var yo = func(_taggedTemplateLiteral([,+,\\\
=,],[,+,\\\\
= ,]),x,y,x + y);






我看到返回的是 var yo = func(strings,raw,x,y,x + y);



我了解字符串文字的基础知识并插入 x y 值。我不明白的是...什么时候使用字符串,什么时候是原始的?由于该函数具有两个数组,并且用户无法控制该函数何时使用raw和何时使用熟(字符串)。

解决方案

标签函数 func 只传递一个数组。该数组来自 _taggedTemplateLiteral 函数,它接收传入的strings参数,并向其添加一个属性。



函数 func 将被声明为这样(ES5风格):

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

如果在 func ,需要使用raw字符串的代码,它将只访问strings变量的 .raw 属性:

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

var raw2 = strings.raw [2];

//做东西
}

所以用户—标签功能的作者— 确实有控制权。标签功能中的代码随时可以随时检查模板部件的原始内容。可能的情况是,基本上实现DSL的标签功能可能只希望使用原始字符串,而较简单的模板机制将不会太在意,并将乐意使用已解析的字符串。 p>

After studying this Es6 tag template example:

var yo = func`${x} + ${y}\n= ${x + y}`;


one@public-node ~/es6 $ 6to5 tag.js 
"use strict";

var _taggedTemplateLiteral = function (strings, raw) {
  return Object.freeze(Object.defineProperties(strings, {
    raw: {
      value: Object.freeze(raw)
    }
  }));
};

var yo = func(_taggedTemplateLiteral(["", " + ", "\n= ", ""], ["", " + ", "\\n= ", ""]), x, y, x + y);


I see what is returned is var yo = func(strings, raw, x, y, x + y);

I understand the basics about the string literals and the x y values being inserted. What I don't understand is...when is strings used versus when is raw used? Since the function has both arrays and the user doesn't have control to tell the function when to use raw and when to use cooked(strings).

解决方案

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.

这篇关于Javascript Es6标签模板 - 什么时候使用?什么时候煮熟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆