JavaScriptã§å¤æ°ãå±éããæååãä½æããæã+
ã§æååé£çµããã¦ãã¾ãããES2015(ES6)ããã¯``
(ããã¯ã¯ã©ã¼ã)ã§å²ããã³ãã¬ã¼ãæ§æ(Template literal)ã§æ¸ããã¨ãã§ããããã§ãã
â» IEãAndroidã§ã¯æªå¯¾å¿ãªãã©ã¦ã¶ããããããªã®ã§ãWEBãµã¤ãå¶ä½ã¨ãã®å ´åã¯ã¾ã ãããå¿
è¦ãããããã§ãã
ãã©ã¦ã¶å®è£
ç¶æ³: ãã³ãã¬ã¼ãæåå - JavaScript | MDN
ãã³ãã¬ã¼ãæ§æ(Template literal)ã«ããæååä¸ã§ã®å¤æ°å±é
var val = "JavaScript" // ä»ã¾ã§ã®ããæ¹ var str1 = "Hello " + val + "!" // => Hello JavaScript! // ãã³ãã¬ã¼ãæ§æ var str2 = `Hello ${val}!` // => Hello JavaScript!
æååä¸ã§å¤æ°ãå±éããããã¬ã¼ã¹ãã«ãã¼ã¯${...}
ã®å½¢å¼ã§è¨è¿°ãã¾ãã
ãã¬ã¼ã¹ãã«ãã¼å ã§è¨ç®
ãã¬ã¼ã¹ãã«ãã¼${...}
å
ã§ã¯è¨ç®ãé¢æ°ãå¼ã¶ãã¨ãã§ããããã§ãã
var a = 3, b = 5 console.log( `a + b = ${a + b}, a * 2 + b = ${a * 2 + b}` ) // => a + b = 8, a * 2 + b = 11 function sub(a, b) { return a - b } console.log( `a - b = ${sub(a, b)}` ) // => a - b = -2
éåæçãªãã¨ã¯ã§ããªã
function timer() { setTimeout(()=> { return 'timeout!' }, 100) } console.log( `set timer ... ${timer()}` ) // => set timer ... undefined
æ¹è¡
ãã³ãã¬ã¼ãæ§æ(Template literal)å
ã§ã¯æ¹è¡ããã®ã¾ã¾æ¹è¡ã¨ãã¦æ±ãããã(\n
ã使ãã)
var str1 = "Hello\nJavaScript" // â var str2 = `Hello JavaScript` var str3 = `Hello\nJavaScript` console.log(str2) // Hello // JavaScript console.log(str3) // Hello // JavaScript
ã³ã¼ãä¸ã§æ¹è¡ã ãããããæ
è¦ãããããã¨ãã§ã³ã¼ãä¸ã§ã ãæ¹è¡ãããããããªå ´å
var tag1 = "<div>" + "<p>Hello JavaScript</p>" + "</div>" console.log(tag1) // => <div><p>Hello JavaScript</p></div> var tag2 = "<div>\ <p>Hello JavaScript</p>\ </div>" console.log(tag2) // => <div><p>Hello JavaScript</p></div>
ãã³ãã¬ã¼ãæ§æ(Template literal)ã§ã¯æ¹è¡ã¯ãã®ã¾ã¾æ¹è¡ã«ãªã£ã¦ãã¾ãã®ã§ãæ¹è¡åã«\
ãä»ãã¦æ¹è¡ããã°OKã(以åã®æ¹æ³ã¨åã)
var tag = `<div>\ <p>Hello JavaScript</p>\ </div>` console.log(tag) // => <div><p>Hello JavaScript</p></div>
ã¨ã¹ã±ã¼ã
\
ã\n
ããã®ã¾ã¾è¡¨ç¤ºããããããªå ´åããã³ãã¬ã¼ãæ§æ(Template literal)ã§ã¯String.raw
ã使ãã(\
ã§ã¨ã¹ã±ã¼ãããä»ã¾ã§ã©ããã®æ¹æ³ã§ãOK)
var str1 = "Hello\\nJava\\Script" // â var str2 = String.raw`Hello\nJava\Script` var str3 = `Hello\\nJava\\Script` console.log(str1 === str2, str1 === str3, str2 === str3) // => true true true console.log(str2) // => Hello\nJava\Script
String.rawå
ã§ããã¬ã¼ã¹ãã«ãã¼${...}
ã¯å±éããã
var name = "ã¢ã·ãªã" String.raw`ããã«ã¡ã\n${name}ãã` // => ããã«ã¡ã¯\nã¢ã·ãªããã
${...}
ããã®ã¾ã¾è¡¨ç¤ºããããå ´åã¯String.raw
ã使ããã«\${...}
åã¯$\{...}
ã¨ã¨ã¹ã±ã¼ããã
var name = "ã¢ã·ãªã" `ããã«ã¡ã¯\\n\${name}ãã` // => ããã«ã¡ã¯\n${name}ãã `ããã«ã¡ã¯\\n$\{name}ãã` // => ããã«ã¡ã¯\n${name}ãã
String.raw
ã使ãã¨\${...}
ã$\{...}
ã®\
ãåºåããã¦ãã¾ã
var name = "ã¢ã·ãªã" String.raw`ããã«ã¡ã¯\n\${name}ãã` // => ããã«ã¡ã¯\n\${name}ãã String.raw`ããã«ã¡ã¯\n$\{name}ãã` // => ããã«ã¡ã¯\n$\{name}ãã
String.raw``ã®ãã©ãã
ããã¹ãã®æå¾ã«\
ãåºåãããå ´åString.raw
ã ã¨æå¾ã®`
ãã¨ã¹ã±ã¼ããã¦ããã¨å¤æããã¨ã©ã¼ã«ãªãã®ã§æ³¨æãå¿
è¦
"Hello\\nJava\\Script\\" // => Hello\nJava\Script\ `Hello\\nJava\\Script\\` // => Hello\nJava\Script\ String.raw`Hello\nJava\Script\` // => Error
String.raw
ã使ã£ã¦æå¾ã«\
ãåºåããã«ã¯\
ãæååçµåãããããã¬ã¼ã¹ãã«ãã¼ã§è¿½å ããã°OK
String.raw`Hello\nJava\Script` + "\\" // => Hello\nJava\Script\ var suffix = "\\" String.raw`Hello\nJava\Script${suffix}` // => Hello\nJava\Script\
ã¨ã¹ã±ã¼ããã¦è¡¨ç¤ºãããæã¯ä»ã¾ã§ã©ããã®\
ã§ã¨ã¹ã±ã¼ãããæ¹æ³ã®æ¹ãè¯ãããããâ¦
String.raw()
éçã¡ã½ããã§ãã
String.raw()
ã¯ãæååãªãã©ã«ã®ããã® Python ã®r
ãã¬ãã£ãã¯ã¹ã C# ã®@
ãã¬ãã£ãã¯ã¹ã®ãã㪠template strings ã®ã¿ã°é¢æ°ã§ãããã®é¢æ°ã¯ãtemplate strings ã®çã®æååå½¢å¼ãåå¾ããããã«ä½¿ç¨ããã¾ãã
åºå ¸: String.raw() - JavaScript | MDN
String.raw
ã¯é¢æ°ãªã®ã§ãfunction``
ã§é¢æ°ãå®è¡ãããã
ãã³ãã¬ã¼ãæ§æ(Template literal)ã§å®è¡ãããé¢æ°ã«æ¸¡ãããå¼æ°
ãã³ãã¬ã¼ãæ§æ(Template literal)ã§æ¸¡ããã第ä¸å¼æ°ã¯æååããã¬ã¼ã¹ãã«ãã¼ã§åå²ããArray-likeãªå¤æ´ä¸å¯è½ãªãªãã¸ã§ã¯ãã第äºå¼æ°ä»¥éã¯ãã¬ã¼ã¹ãã«ãã¼ã«å ¥ãå¤ãå¤ã¯è¨ç®ãããç¶æ ã§é¢æ°ã«æ¸¡ãããã£ã½ãã
var a = 5, b = 10 function tag(strings, ...values) { console.log(strings) // å¤æ´ä¸å¯ãªãã¸ã§ã¯ã console.log(values) // ãã¬ã¼ã¹ãã«ãã¼ã®å¤ return strings } var str1 = tag`Hello${a+b}Java${a-b}script` // strings: [ 'Hello', 'Java', 'script' ] // vales: [ 15, -5 ] var str2 = tag`Hello${b-a}Java${a*b}script` // strings: [ 'Hello', 'Java', 'script' ] // values: [ 5, 50 ] console.log(str1 === str2) // true console.log(typeof(str1)) // 'object' // å¤æ´ãããã¨ããã¨ã¨ã©ã¼ã«ãªã str1[1] = 'World' // => TypeError: Cannot assign to read only property delete str1[2] // => TypeError: Cannot delete property str1.push('!') // => TypeError: Cannot add property
å
¬å¼ãè¦ãã¨ã¯ãã¼ã¸ã£ã¼ãªé¢æ°ãè¿ãã¨ãè²ã
ãªä½¿ãæ¹ãã§ããã£ã½ããã©ãå®éã«ä½¿ãå ´é¢ãã¤ãã¤ããã³ããªã...
ã
ã¯ã¦ãªããã°ã®JavaScriptããæãã«ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ããããªãã¦è¾ãâ¦
[åè]
- テンプレートリテラル (テンプレート文字列) - JavaScript | MDN
- JavaScript の テンプレートリテラル を極める! #ECMAScript - Qiita
- String.raw() - JavaScript | MDN
ãã¼ã¯ãã¦ã³ã§`
ããã¯ã¯ã©ã¼ããcodeã§å²ã£ã¦æ¸ãTips