多语言展示
当前在线:1909今日阅读:84今日分享:32

如何使用js中的String.raw 函数

String.raw 函数  返回模板字符串的原始字符串形式。
工具/原料
1

浏览器

2

网页编程工具

方法/步骤
1

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

2

参数templateStr必需。模板字符串。obj必需。一个使用对象文本表示法指定的格式正确的对象,例如 { raw: “value” }。...substitutions可选。一个数组(rest 参数),包含一个或多个替换值。

3

备注String.raw 函数旨在与模板字符串一起使用。原始字符串将包含存在于字符串中的任何转义字符和反斜杠。如果 obj 不是格式正确的对象,则会引发错误。

4

示例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

5

要求Microsoft Edge(Edge 浏览器)中受支持。应用商店应用(Windows 10 上的 Microsoft Edge)中也受支持。请参阅版本信息。在以下文档模式中不受支持:Quirks、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式、Internet Explorer 9 标准模式、Internet Explorer 10 标准模式和 Internet Explorer 11 标准模式。在 Windows 8.1 中不受支持。

注意事项

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

推荐信息