静的メソッドである String.raw() は、文字列リテラルのための Python の r プレフィックスや C# の @ プレフィックスのような template strings のタグ関数です。この関数は、template strings の生の文字列形式を取得するために使用されます。
構文
String.raw(callSite, ...substitutions)
String.raw`templateString`
引数
callSite{ raw: 'string' }のような、よく形成されたテンプレート呼び出しサイトオブジェクト。...substitutions- 置換値
templateString- template string。必要に応じて置換します (
${...})。
エラーのスロー
説明
ほとんどの場合、String.raw() は template strings とともに使用されます。上記の最初の構文は、JavaScript エンジンが、他の タグ関数 のように、適切な引数で呼び出すので、本当に使用されます。
String.raw() は template strings の唯一のビルトインタグ関数です。デフォルトテンプレート関数のように動作し、連結を行います。通常の JavaScript コードで再実装することができます。
例
String.raw() を使う
String.raw`Hi\n${2+3}!`;
// 'Hi\\n5!', the character after 'Hi' is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective and
// backslashes will be present in the output string.
// You can confirm this by checking the .length property of the string.
let name = 'Bob';
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
// Normally you would not call String.raw() as a function, but you can do so:
String.raw({ raw: 'test' }, 0, 1, 2);
// 't0e1s2t'
仕様
| 仕様 | 状況 | コメント |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) String.raw の定義 |
標準 | 初回定義。 |
| ECMAScript 2017 Draft (ECMA-262) String.raw の定義 |
ドラフト |
ブラウザ実装状況
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基本サポート | 41 | 34 (34) | 未サポート | 未サポート | 未サポート |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | 未サポート | 41 | 34.0 (34) | 未サポート | 未サポート | 未サポート |