String.raw 函数

返回模板字符串的原始字符串形式。

语法

String.raw`templateStr`;
String.raw(obj, ...substitutions);

参数

templateStr

必要参数。模板字符串。

obj

必要参数。一个使用对象文本表示法指定的格式正确的对象,例如{ raw:"value" }。

...substitutions

可选参数。一个数组(rest 参数),包含一个或多个替换值。

备注

String.raw函数旨在与模板字符串一起使用。原始字符串将包含存储于字符串中的任何转义字符和反斜杠。

如果obj不是格式正确的对象,则会引发错误。

示例

function log(arg) {
  if(console && console.log) {
    console.log(arg);
  }
};
var name = "bob";
log(`hello \t${name}`);
log(String.raw`hello \t${name}`);
// The following usage for String.raw is supported but
// is not typical.
log(String.raw({ raw: 'fred'}, 'F', 'R', 'E'));
// Output:
// hello  bob
// hello \tbob
// fFrReEd

必备条件

Microsoft Edge中受支持。应用商店应用(Windows 10 上的 Microsoft Edge)中也受支持。请参阅版本信息

在以下文档模式中不受支持:怪异模式、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式、Internet Explorer 9 标准模式、Internet Explorer 10 标准模式和 Internet Explorer 11 标准模式。在 Windows 8.1 中不受支持。

如果你喜欢这篇文章,敬请给站长打赏↑

除特别注明外,本站所有文章均为本站站长原译,转载请注明出处。