String.raw
String.raw()
์ ์ ๋ฉ์๋๋ template Iiterals์ ํ๊ทธ ํจ์์ด๋ค. Python์ r
prefix์ ๋ฌธ์์ด ๋ฆฌํฐ๋ด์ ๊ฒฝ์ฐ C#์ @ ์ ๋์ฌ์ ์ ์ฌํ๋ค. (๊ทธ๋ฌ๋ ๋์ผํ์ง ์์) template ๋ฌธ์์ด์ ์์ ๋ฌธ์์ด ํํ, ๋์ฒด(์ ${foo}) ์ฒ๋ฆฌ๊ฐ ๋์ง๋ง ์ด์ค์ผ์ดํ(์: \n)๋ ์ฒ๋ฆฌ๋์ง ์๋๋ค.
// "C:Developmentprofileaboutme.html"
// raw๋ฅผ ์ฌ์ฉํ์ง ์์์ ๋
`C:\Development\profile\aboutme.html`;
// ๋ฐฑ์ฌ๋์๊ฐ ๋น ์ง์ง ์๊ฒ ์๋์ฐ ํ์ผ ๊ฒฝ๋ก๋ฅผ ๋ด๋ ๋ณ์๋ฅผ ๋ง๋ค์ด๋ผ
const filePath = String.raw`C:\Development\profile\aboutme.html`;
console.log('The file was uploaded from:' + filePath); // The file was uploaded from:C:\Development\profile\aboutme.html
Syntax
String.raw(callSite, ...substitutions)
string.raw`templateString`
Parameters
callSite
{ raw: ['foo', 'bar', 'baz'] }
๊ฐ์ด ์ ํ์ฑ๋ template๋ฅผ ํธ์ถํ๋ site object
...substitution
template string, ํ์ํ ๊ฒฝ์ฐ ๊ต์ฒดํ๋ค (${...}
).
Return value
์ฃผ์ด์ง template ๋ฌธ์์ด์ ์์ ๋ฌธ์์ด์ด๋ค.
์์ธ
site object๊ฐ ์๋ชป๋ ํ์ฑ์ด๋ฉด TypeError
(๊ฐ์ด ๊ธฐ๋ํ๋ ์๋ฃํ์ด ์๋๋ผ์ ์ฐ์ฐ์ ํ ์ ์์ ๋ ๋ฐ์ํ๋ ์ค๋ฅ) ๊ฐ ๋ฐ์ํ๋ค.
Description
๋๋ถ๋ถ์ ๊ฒฝ์ฐ, String.raw()
๋ template string๊ณผ ๊ฐ์ด ์ฌ์ฉ๋๋ค.
์์์ ์ธ๊ธํ ์ฒซ๋ฒ์งธ ๊ตฌ๋ฌธ์ ๊ฑฐ์ ์ฌ์ฉ๋์ง ์๋๋ค. JavaScript ์์ง์ด (๋ค๋ฅธ ํ๊ทธ ๊ธฐ๋ฅ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก) ์ ์ ํ ์ธ์๋ก ํธ์ถํ๊ธฐ ๋๋ฌธ์ด๋ค.
String.raw()
ํ
ํ๋ฆฟ ๋ฌธ์์ด์ ์ ์ผํ built-in tag function์ด๋ค. ๊ธฐ๋ณธ ํ
ํ๋ฆฟ ๊ธฐ๋ฅ๊ณผ ๋์ผํ๊ฒ ์๋ํ๋ฉฐ ์ฐ๊ฒฐ์ ์ํํ๋ค. JavaScript ์ฝ๋๋ก ๋ค์ ๊ตฌํํ ์ ์๋ค.
String.raw() ์ฌ์ฉ
`Hi\n${2+3}!` /* "Hi
5!" */
String.raw`Hi\n${2+3}!`;
// 'Hi' ๋ค์์ ์ค๋ ๋ฌธ์์ด์ ์ค๋ฐ๊ฟ์ด ์ผ์ด๋์ง ์๋๋ค.
`Hi\u000A!`; /* "Hi
!" */
String.raw`Hi/u000A!`; // String.raw`Hi\u000A!`;
// escape character(\', \", \\, \n, \r, \t, \b, \v, \0, \xFF)๋ ํจ๊ณผ๊ฐ ์๋ค. ๋ฐฑ์ฌ๋์๋ ์ถ๋ ฅ
// ๋ฌธ์์ด์ .length ์์ฑ์ ์ฌ์ฉํ์ฌ ํ์ธํ ์ ์๋ค.
`Hi\u000A!`.length // 4
String.raw`Hi/u000A!`.length // 9
let name = 'Bob';
String.raw`Hi\n${name}!`; // "Hi\nBob!" ${name}๋ ์ฒ๋ฆฌ๊ฐ ๋๋ค.
// ์ผ๋ฐ์ ์ผ๋ก String.raw()๋ฅผ ํจ์๋ก ํธ์ถ ํ์ง ์๋๋ค.
// 't${0}e${1}s${2}t'์(๋ฅผ) ์๋ฎฌ๋ ์ด์
ํ๋ ค๋ฉด ์๋์ ๊ฐ์ด..
String.raw({raw: 'test'}, 0, 1, 2); // "t0e1s2t"
// ๋ฌธ์์ด์ธ 'test'๋ ๋ฐฐ์ด๊ณผ ์ ์ฌํ ๊ฐ์ฒด์ด๋ค.
// `foo${2 + 3}bar${'Java' + 'Script'}baz`์ ํด๋นํ๋ค.
String.raw({raw: ['foo', 'bar', 'baz']}, 2 + 3, 'Java' + 'Script'); // "foo5barJavaScriptbaz"
Browser compatibility
IE ๋, Opera ๋, ์๋๋ก์ด๋ ์น๋ทฐ ๋, ์๋๋ก์ด๋ ์คํ๋ผ ๋
Last updated
Was this helpful?