Baseline Widely available
El método estatico String.raw()
es una función de plantilla de literales, similar al prefijo r
en Python o al prefijo @
en C# para strings literales (con ciertas diferencias: ver la explicación en este problema). Se utiliza para obtener un string crudo a partir de plantillas de string (es decir, el original, texto no interpretado).
String.raw(callSite, ...substitutions) String.raw`templateString`Parametros
callSite
Plantilla bien estructurada, similar a { raw: ['foo', 'bar', 'baz'] }
.
...substitutions
Contiene valores de sustitución.
templateString
[opcional] Una plantilla string, con sustituciones (${...}
).
La forma cruda del string de una plantilla string proporcionada.
ExcepcionesTypeError
Un TypeError
es arrojado si el primer argumento no es un objeto bien estructurado.
En la mayorÃa de los casos, String.raw()
es usado con plantillas string. La primera sintaxis mencionada arriba es raramente usada, porque el motor de JavaScript hará la llamada por ti con los argumentos apropiados, al igual que otras funciones de etiqueta.
String.raw()
es la unica función de etiqueta incorporada en las plantillas string; trabaja igual que la función de la plantilla por defecto y ejecuta la concatenación. Incluso puedes reimplementarlo con código normal de 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 to simulate `t${0}e${1}s${2}t` you can do:
String.raw({ raw: "test" }, 0, 1, 2); // 't0e1s2t'
// Note that 'test', a string, is an array-like object
// The following is equivalent to
// `foo${2 + 3}bar${'Java' + 'Script'}baz`
String.raw(
{
raw: ["foo", "bar", "baz"],
},
2 + 3,
"Java" + "Script",
); // 'foo5barJavaScriptbaz'
Especificaciones Compatibilidad con navegadores Tambien ver
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4