본문 바로가기

자바스크립트의 정석 🟡

자바스크립트 - tag function

참고자료: 인프런 자바스크립트 초급강의

 

Javascript ES6+ 제대로 알아보기 - 초급 - 인프런 | 강의

ES6+ 제대로 알아보기 강좌는 Javascirpt의 ES6 및 이후의 표준 ECMAScript 명세에 대하여 이론을 바탕으로 ES5와 달라진 점 및 개념과 동작 원리를 깊이 있게 알려드립니다., Javascript ES6+는 뭐가 다를까?

www.inflearn.com

 

tag function 

const tag = function (strs, arg1, arg2) {
  return {strs: strs, args: [arg1, arg2]}
}

기본형태

 

 

예시

const tag = function (strs, arg1, arg2) {
  return {strs: strs, args: [arg1, arg2]}
}
const res = tag `순서가 ${1}이렇게 ${2}`
console.log(res)

//result
args (2) [1, 2]
strs (3) ['순서가 ', '이렇게 ', '', raw: Array(3)]

문자열은 항상 숫자열보다 하나 더 많음. -> 빈칸까지 받아들임

 

 

참고:

string.raw()

console.log(`Hello\nWorld!`)
console.log(String.raw `Hello\nWorld!`)
console.log(String.raw `Hello
World!`)

//result
Hello
World!

 Hello\nWorld!
 
 Hello
World!

string.raw()는 띄어쓰기 문법까지 그대로 결과값으로 출력

 

알아두셈