λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
[개발] Practice/JavaScript ES6

[JavaScript / ES6] λ³€μˆ˜ 신문법 μ΄μ •λ¦¬ν•˜κΈ°_2

by Connecting-the-dots 2022. 4. 11.
728x90
λ°˜μ‘ν˜•

πŸ’‘ μ‹€μŠ΅ 포인트!

πŸ’œ JavaScript μ—μ„œμ˜ λ³€μˆ˜, ν•¨μˆ˜ Hoisting ν˜„μƒ μ•Œμ•„λ³΄κΈ°

  • JavaScript λŠ” λ³€μˆ˜λ‚˜ ν•¨μˆ˜λ₯Ό μ„ μ–Έν•˜λ©΄, Hoisting μ΄λΌλŠ” ν˜„μƒμ΄ λ°œμƒν•œλ‹€.
  • Hoisting μ΄λž€ λ³€μˆ˜λ‚˜ ν•¨μˆ˜μ˜ μ„ μ–Έ λΆ€λΆ„λ§Œ λ³€μˆ˜μ˜ λ²”μœ„ 맨 μœ„λ‘œ κ°•μ œλ‘œ 끌고 μ˜¬λΌκ°€μ„œ κ°€μž₯ λ¨Όμ € ν•΄μ„ν•˜λŠ” 것을 λ§ν•œλ‹€.
function func(){
  
  console.log('hello');
  var name = 'Seoyun';
  
}
  • 예λ₯Ό λ“€μ–΄, μœ„μ™€ 같은 func() ν•¨μˆ˜κ°€ 있고, κ·Έ ν•¨μˆ˜ μ•ˆμ— name μ΄λΌλŠ” λ³€μˆ˜λ₯Ό λ§Œλ“€μ—ˆλ‹€κ³  κ°€μ •ν•˜μž.

 

function func(){

  var name;
  console.log('hello');
  name = 'Seoyun';
  
}
  • 그럼 JavaScript κ°€ func() ν•¨μˆ˜μ˜ μ½”λ“œλ₯Ό ν•΄μ„ν•˜λŠ” μˆœμ„œλŠ” μœ„μ™€ κ°™λ‹€.
  • λ³€μˆ˜μ˜ μ„ μ–Έ 뢀뢄인 var name 을 κ°•μ œλ‘œ λ³€μˆ˜μ˜ λ²”μœ„ 맨 μœ„λ‘œ 끌고 μ˜¬λΌκ°€μ„œ ν•΄μ„ν•˜κ³  μ§€λ‚˜κ°€λŠ” 것이닀.
  • μ΄λŸ¬ν•œ Hoisting ν˜„μƒμ€ ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄λ„, λ³€μˆ˜λ₯Ό let, const ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄λ„ λ˜‘κ°™μ΄ λ°œμƒν•œλ‹€.

πŸ’œ λ³€μˆ˜ μ—¬λŸ¬ 개 λ§Œλ“€κΈ°

var name, age, sex;
  • λ³€μˆ˜λ₯Ό 콀마( , ) 둜 κ΅¬λΆ„ν•˜λ©΄ μ—¬λŸ¬ 개λ₯Ό λ™μ‹œμ— λ§Œλ“€ 수 μžˆλ‹€.
  • μœ„μ™€ 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ var ν‚€μ›Œλ“œλ₯Ό λ³€μˆ˜μ˜ 개수만큼 μ‚¬μš©ν•˜μ§€ μ•Šμ•„λ„ λ³€μˆ˜κ°€ 3κ°œκ°€ 생성이 λœλ‹€.

 

var name = 'Seoyun', age, sex;

 

  • 일뢀 ν˜Ήμ€ 전체 λ³€μˆ˜μ— μ„ μ–Έκ³Ό λ™μ‹œμ— 할당을 ν•˜κ³  μ‹ΆμœΌλ©΄ μœ„μ™€ 같은 ν˜•μ‹μœΌλ‘œ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ λœλ‹€.

πŸ’œ μ „μ—­λ³€μˆ˜μ™€ λ³€μˆ˜μ˜ μ°Έμ‘° μ•Œμ•„λ³΄κΈ°

🀍 λ³€μˆ˜μ˜ μ°Έμ‘° μ•Œμ•„λ³΄κΈ°

var age = 20;

function showAge(){
  console.log(age)
}

showAge();  // 20
  • λ³€μˆ˜μ˜ νŠΉμ§• μ€‘μ—λŠ” λ°”κΉ₯의 λ³€μˆ˜λŠ” μ•ˆμ—μ„œ 자유둭게 μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€λŠ” 점이 μžˆλ‹€.
  • 이것을 κ°œλ°œμžμš©μ–΄λ‘œ μ°Έμ‘° κ°€λŠ₯ν•˜λ‹€λΌκ³  ν‘œν˜„ν•˜λŠ”λ°, JavaScript μ—μ„œλŠ” 이 ν˜„μƒμ„ closure 라고 ν•œλ‹€.
  • 예λ₯Ό λ“€μ–΄, μœ„μ™€ 같은 μ½”λ“œκ°€ 있으면 showAge() ν•¨μˆ˜ 밖에 μžˆλŠ” age λ₯Ό showAge() ν•¨μˆ˜ μ•ˆμ—μ„œ κ°€μ Έλ‹€ μ‚¬μš©ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€λŠ” 것이닀.
  • λ§Œμ•½, showAge() ν•¨μˆ˜ μ•ˆμ— age λΌλŠ” λ³€μˆ˜λ₯Ό μ •μ˜ν•΄λ†“μ€ 게 μžˆλ‹€λ©΄ κ·Έ λ³€μˆ˜λ₯Ό κ°€μ Έλ‹€ μ‚¬μš©ν•˜κ² μ§€λ§Œ, μ—†μœΌλ©΄ μžμ—°μŠ€λŸ½κ²Œ 밖에 μžˆλŠ” λ³€μˆ˜λ₯Ό κ°€μ Έλ‹€ μ°Έμ‘°ν•˜μ—¬ μ‚¬μš©ν•œλ‹€.

🀍 μ „μ—­λ³€μˆ˜ μ•Œμ•„λ³΄κΈ°

<script>
  var age = 20;

  function showAge(){
    console.log(age)
  }
</script>
  • ν”„λ‘œκ·Έλž˜λ°μ—μ„œλŠ” μ „μ—­λ³€μˆ˜λΌλŠ” 것이 μžˆλ‹€.
  • μ „μ—­λ³€μˆ˜λž€, λͺ¨λ“  ν•¨μˆ˜λ‚˜ if λ¬Έ, ν˜Ήμ€ for λ¬Έ λ‚΄λΆ€μ—μ„œ κ³΅ν†΅μ μœΌλ‘œ μ‚¬μš©(μ°Έμ‘°)ν•  수 μžˆλŠ” μœ μš©ν•œ λ³€μˆ˜λ₯Ό μ˜λ―Έν•œλ‹€.
  • μ „μ—­λ³€μˆ˜λ₯Ό λ§Œλ“€μ–΄ μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄ <script></script> μ•ˆμ˜ 제일 λ°”κΉ₯ μ˜μ—­μ— λ³€μˆ˜λ₯Ό λ§Œλ“€μ–΄μ£Όλ©΄ λœλ‹€. 
  • μœ„μ™€ 같이 age λΌλŠ” μ „μ—­λ³€μˆ˜λ₯Ό λ§Œλ“€μ–΄λ‘λ©΄, κ·Έ 밑에 for λ¬Έ, if λ¬Έ, ν•¨μˆ˜ λ“±μ—μ„œ λͺ¨λ‘ age λΌλŠ” λ³€μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

 

<script>
  var age = 20;
  
  console.log(age);  // 20
  console.log(window.age);  // 20
</script>
  • λ§Œμ•½ var ν‚€μ›Œλ“œλ‘œ μ „μ—­λ³€μˆ˜λ₯Ό λ§Œλ“€λ©΄ μ΄λŠ” window λΌλŠ” object 에도 보관이 λœλ‹€.
  • λ”°λΌμ„œ, μœ„ κ²½μš°μ— window.age 와 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ„ var ν‚€μ›Œλ“œλ‘œ λ§Œλ“  age 와 λ™μΌν•˜κ²Œ 좜λ ₯이 κ°€λŠ₯ν•˜λ‹€.

 

<script>
  window.age = 20;  // μ „μ—­λ³€μˆ˜ λ§Œλ“€κΈ°
  console.log(window.age);  // μ „μ—­λ³€μˆ˜ μ‚¬μš©ν•˜κΈ°
  window.age = 30;  // μ „μ—­λ³€μˆ˜ λ³€κ²½ν•˜κΈ°
</script>
  • μ „μ—­λ³€μˆ˜λ₯Ό μ—„κ²©ν•˜κ²Œ κ΄€λ¦¬ν•˜κ±°λ‚˜ κ΅¬λΆ„ν•˜κ³  μ‹Άλ‹€λ©΄, μ „μ—­λ³€μˆ˜λ₯Ό λ§Œλ“€ λ•Œ ν˜Ήμ€ μ‚¬μš© 및 λ³€κ²½ν•  λ•Œ window λ₯Ό ν™œμš©ν•΄λ³΄λŠ” 것도 μ’‹λ‹€.
  • node.js ν™˜κ²½μ—μ„œλŠ” window λŒ€μ‹  global μ΄λΌλŠ” object λ₯Ό ν™œμš©ν•  수 μžˆλ‹€.

 

728x90
λ°˜μ‘ν˜•