JavaScript | pre course 27-[ES6] template literals, string method(+handleEdit)`

ํƒœํ˜„ยท2021๋…„ 3์›” 27์ผ
0
post-thumbnail

๐Ÿ™‹ ๋ชฉํ‘œ: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ๋ฅผ ๊ณต๋ถ€ํ•˜๊ณ  ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์„ ์ฑ„์šฐ์ž.

1. template literals


๐Ÿ™Œ ES6์—์„œ ์ถ”๊ฐ€๋œ ๋ฌธ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” template literals์ด๋‹ค.

์›๋ž˜ ๋ฌธ์žํ˜•์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ์ง€๋งŒ, ์ด์ œ๋Š” ๋ฐฑํ‹ฑ()` ์ด๋ผ๋Š” ๊ธฐํ˜ธ๋กœ ๋ฌธ์žํ˜•์„ ๊ฐ์‹ธ์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

const chat = `์•ˆ๋…•ํ•˜์„ธ์š”, ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค.`

๊ทธ๋ฆฌ๊ณ  ๋ฐฑํ‹ฑ()์˜ ๊ฐ€์žฅ ํฐ ์žฅ์ ์€๋ฐฑํ‹ฑ() ๊ณผ ๋‹ฌ๋Ÿฌ์‚ฌ์ธ($)`๊ทธ ์•ˆ์— ์šฐ๋ฆฌ๊ฐ€ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹น์—์„œ ๊ฐ™์ด ๋ฌธ์žํ˜•์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

const age = 11;
const chat = `์•ˆ๋…•ํ•˜์„ธ์š”, ์ €๋Š” ${age}์‚ด ์ž…๋‹ˆ๋‹ค.`;
  • ์ค„๋ฐ”๊ฟˆ๋„ ๊ฐ€๋Šฅํ•œ template literal
const chat = `์ค„๋ฐ”๊ฟˆ๋„
๊ฐ€๋Šฅํ•œ
ES6, 
template literals`;

2. string method


ES6์—์„œ ์ถ”๊ฐ€๋œ ๋ช‡ ๊ฐ€์ง€ ์œ ์šฉํ•œ ๋ฌธ์žํ˜• ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค.

์ด๊ฒƒ์€ ๋‹จ์ˆœ์ด ๋ฌธ์žํ˜•์—์„œ ํŠน์ • ๋ฌธ์žํ˜•์„ ์ฐพ๊ธฐ ์œ„ํ•ด indexOf ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋‹ค๋ฅด๋‹ค.

  • ES6์— ์ถ”๊ฐ€๋œ ๋ฌธ์žํ˜• ๋ฉ”์†Œ๋“œ
    • startsWith()
    • endsWith()
    • includes()
const email = 'office.div1201@gamil.com';

console.log(email.startsWith('office')); //true
console.log(email.endsWith('com')); //true
console.log(email.includes('v1201')) //true

3. Assignment


handleEdit ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์ž.

  • ์‡ผํ•‘๋ชฐ ํšŒ์›๊ฐ€์ž…์„ ์ง„ํ–‰ํ•˜๋ฉด ์ฟ ํฐ ์ ๋ฆฝ๊ณผ ๊ฐ™์€ ๋ฌธ๊ตฌ๊ฐ€ ๋œฌ ์ ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

    ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ์œ„์ฝ”๋“œ๋‹˜!
    ํšŒ์›๊ฐ€์ž… ๊ธฐ๋…์œผ๋กœ ์ฟ ํฐ์„ ์ ๋ฆฝํ•ด ๋“œ๋ ธ์Šต๋‹ˆ๋‹ค.

    ์ด๋Ÿฌํ•œ ๋ฌธ๊ตฌ์—์„œ๋Š” ์ด๋ฆ„ ๋ถ€๋ถ„๋งŒ ๊ณ„์† ๋ฐ”๋€Œ๊ณ  ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ๊ณ ์ •๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ, ์ด๋ฆ„ ๋ถ€๋ถ„์„ ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌ ํ•˜๋Š” ๊ฒƒ๐ŸŽฏ

  • ๊ทธhandleEditย ๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์ž.

  • ์ด ํ•จ์ˆ˜๋Š”ย nickname,ย interestsย ๋ผ๋Š” ๋‘ string์„ ์ธ์ž๋กœ ๋ฐ›๋Š”๋ฐย nickname์€ ์œ ์ €์˜ ๋‹‰๋„ค์ž„์„,ย interestsย ๋Š” ์œ ์ €์˜ ๊ด€์‹ฌ์‚ฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

  • interestsย ์—๋Š” ์—ฌ๋Ÿฌ ๊ด€์‹ฌ์‚ฌ๋ฅผ ์ ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ๋•Œ ๊ทธ ๊ด€์‹ฌ์‚ฌ์˜ ๊ตฌ๋ถ„์„ ์ฝค๋งˆ(,)๋ฅผ ์ด์šฉํ•œ๋‹ค.

  • nicknameย ๊ณผย interestsย ๊ฐ€ ๋ฌธ์ œ ์—†์ด ์ž˜ ๋“ค์–ด์™”๋‹ค๋ฉด ์œ ์ € ์ •๋ณด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ Object์— ์ด ์ •๋ณด๋ฅผ ๋‹ด์•„์„œ ๊ด€๋ฆฌํ•œ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ ๊ฐ’์ดย nickname = "๋šœ๋น„"ย ,ย interests = "๋ฐฉํƒˆ์ถœ,ํ…Œ๋‹ˆ์Šค,๋ฉ ๋•Œ๋ฆฌ๊ธฐ"ย ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์ž.

โš ๏ธย ์ฃผ์˜

  • interestsย ๋Š” ๋ฐฐ์—ด์ด๋‹ค.

  • interestsย ๋ฅผ ์ ์„ ๋•Œ ์ฝค๋งˆ(,) ๋’ค์— ์ŠคํŽ˜์ด์Šค๋Š” ์ง€์šฐ์ž๐Ÿš€

    {
        nickname: "๋šœ๋น„",
        interests: ["๋ฐฉํƒˆ์ถœ","ํ…Œ๋‹ˆ์Šค","๋ฉ ๋•Œ๋ฆฌ๊ธฐ"],
        bio: "์ œ ๋‹‰๋„ค์ž„์€ ๋šœ๋น„์ž…๋‹ˆ๋‹ค. ์ทจ๋ฏธ๋Š” ๋ฐฉํƒˆ์ถœ,ํ…Œ๋‹ˆ์Šค,๋ฉ ๋•Œ๋ฆฌ๊ธฐ์ž…๋‹ˆ๋‹ค."
    }
    • ํ•ด๊ฒฐ๋ฐฉ๋ฒ•1

      const handleEdit = (nickname, interests) => {
          const handleObj = {}
          handleObj.nickname = nickname
          handleObj.interests = interests.split(',')
          handleObj.bio = `์ œ ๋‹‰๋„ค์ž„์€ ${handleObj.nickname}์ž…๋‹ˆ๋‹ค. ์ทจ๋ฏธ๋Š” ${handleObj.interests}์ž…๋‹ˆ๋‹ค.`
      		return handleObj;
      }
    • ํ•ด๊ฒฐ๋ฐฉ๋ฒ•2(๊ฐœ์„ ๋œ ์ฝ”๋“œ๐Ÿš’)

      const handleEdit = (nickname, interests) => {
          const handleObj = {
              nickname: nickname,
              interests: interests.split(','),
              bio: `์ œ ๋‹‰๋„ค์ž„์€ ${nickname}์ž…๋‹ˆ๋‹ค. ์ทจ๋ฏธ๋Š” ${interests}์ž…๋‹ˆ๋‹ค.`
          }
          return handleObj;
      }
profile
์•ˆ๋…•ํ•˜์„ธ์š”, ์ง€์‹์„ ๊ณต์œ ํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€