javascript console.log new feature with 'raw'?

javascript console.log new feature with 'raw'?

本文关键字:raw with console log new feature javascript      更新时间:2023-09-26

我遇到过这个例子,完全迷路了…

const test = (hey) => console.log(hey);
console.log(test `wtf`); 

首先这些都是有效的,在console.log中,它看起来是

["wtf", raw: Array[1]]

这就像函数被执行和额外的raw ?有人能解释一下吗?

这只是一个标记模板文字。它看起来很别致,但没有什么特别之处。注意,它们是ES6/ES2015的一部分,所以如果你打算支持旧的浏览器,你需要转换它们。

模板字面值是允许嵌入表达式的字符串字面值。您可以使用多行字符串和字符串插值功能。在ES2015/ES6规范的早期版本中,它们被称为"模板字符串"。

感谢@karmuran和@ decze

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals Tagged_template_literals

原始字符串

特殊的raw属性,在标记模板字面值的第一个函数参数中可用,允许您在输入原始字符串时访问它们。

function tag(strings, ...values) {
  console.log(strings.raw[0]); 
  // "string text line 1 'n string text line 2"
}
tag`string text line 1 'n string text line 2`;