JavaScript | pre course 26-ECMA Script 2015

νƒœν˜„Β·2021λ…„ 3μ›” 26일
1
post-thumbnail

πŸ™‹ λͺ©ν‘œ: μžλ°”μŠ€ν¬λ¦½νŠΈ 기초λ₯Ό κ³΅λΆ€ν•˜κ³  λΆ€μ‘±ν•œ 뢀뢄을 μ±„μš°μž.

1. ES6 = ECMA Script 2015


πŸ™Œ ESλŠ” ECMA Script의 μ€„μž„λ§μ΄λ‹€. ES6λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό ν‘œμ€€ν™” μ‹œν‚€κΈ° μœ„ν•΄ λ§Œλ“€μ–΄μ‘Œλ‹€.

μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 역사가 점점 κΉŠμ–΄μ§€κ³ , λ¬Έλ²•μ˜ 보완이 ν•„μš”ν•˜λ©΄μ„œ λ²„μ „λ³„λ‘œ 문법을 ν™•μž₯μ‹œν‚€κ³  λΈŒλΌμš°μ €λ„ μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ νŠΉμ • 버전을 μ§€μ›ν•˜κΈ° ν•˜κΈ° μœ„ν•΄μ„œ ESκ°€ λͺ…μ„Έλ₯Ό ν™•μ •ν•΄ μ™”λ‹€κ³ ν•œλ‹€.

즉, ESλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό ν‘œμ€€ν™”, κ·œκ²©ν™”ν•˜κΈ° μœ„ν•΄ λ§Œλ“€μ–΄μ§„ 것이닀.

μ§€κΈˆμ€ ES10 λ²„μ „κΉŒμ§€ λ‚˜μ™”λ‹€. 주둜 μ‚¬μš©λ˜λŠ” 것은 ES6인데, 6λŠ” 버전 이름이고 ES 2015라고도 ν•œλ‹€. 1~2λ…„ μ „κΉŒμ§€λ§Œ 해도 ES6λ₯Ό μ‚¬μš©ν•˜λŠ” μ‚¬λžŒμ€ λ§Žμ§€ μ•Šμ•˜μ§€λ§Œ, μ§€κΈˆμ€ λ„ˆλ¬΄λ‚˜ λ§Žμ€ μ‚¬λžŒλ“€μ΄ μ“°κ³  μžˆλ‹€. 이세상은 λ„ˆλ¬΄λ‚˜ λΉ λ₯΄κ²Œ λ³€ν™”ν•œλ‹€.πŸš€

ES6λ₯Ό 많이 ν™œμš©ν•˜λ©΄ μ’‹λ‹€κ³  ν•œλ‹€. μš”μ¦˜ ES7을 μ‚¬μš©ν•˜λŠ” κ°œλ°œμžλ“€λ„ 점차 λŠ˜μ–΄λ‚˜κ³  μžˆλ‹€.

ES6λ₯Ό κ³΅λΆ€ν•˜λ‹€λ³΄λ©΄, 정말 νŽΈλ¦¬ν•œ ν•¨μˆ˜λ“€κ³Ό μ²˜μŒλ³΄λŠ” 문법듀도 많이 μƒκ²¨λ‚¬λ‹€λŠ” 것을 λŠλ‚„ 수 μžˆλ‹€. κ·Έλž˜μ„œ 곡뢀가 ν•„μš”ν•˜λ‹€. μ—¬λŸ¬κ°€μ§€ 곡뢀듀 쀑 μ˜€λŠ˜μ€ arrow function이닀.

//ES5
function() {}

//ES6
() => {}

μ΄λ¦„μ—†λŠ” ν•¨μˆ˜, anonymous function을 확인해 λ³Ό 수 μžˆλŠ”λ° ES6λŠ” functionμ΄λΌλŠ” ν‚€μ›Œλ“œκ°€ 빠지고 μ†Œκ΄„ν˜Έλ§Œ λ‚¨μ•˜λ‹€. 그리고 => κ°€ μΆ”κ°€λ˜μ—ˆλ‹€.

//ES5
function getName() {}

//ES6
const getName = () => {}

ν˜ΈμΆœν•  λ•ŒλŠ”

getName()

ES6λŠ” ν•¨μˆ˜λ₯Ό getName μ΄λΌλŠ” λ³€μˆ˜μ— μ €μž₯ν–ˆλ‹€.

사싀 ν•¨μˆ˜λŠ” λ³€μˆ˜μ— μ €μž₯ν•  수 μžˆλ‹€.

κ·Έλž˜μ„œ ES5 일 λ•Œλ„ λ§ˆμ°¬κ°€μ§€λ‘œ λ³€μˆ˜μ— μ €μž₯이 κ°€λŠ₯ν•˜λ‹€.

//ES5
//Function Declaration
function getName() {}

//ES5
//Function Expression
const getName = function() {}

인자λ₯Ό λ°›λŠ”λ‹€λ©΄

//ES5
const getName = function(name) {}

//ES6
const getName = (name) => {}
const getName = name => {}

인자λ₯Ό 두 개 이상 λ°›λŠ”λ‹€λ©΄ (μ†Œκ΄„ν˜Έ) μƒλž΅μ΄ λΆˆκ°€ν•˜λ‹€.

//ES5
const getName = function(name, age) {}

//ES6
const getName = (name, age) => {}

λ¦¬ν„΄ν•˜λŠ” ν•¨μˆ˜λŠ” λ§Œλ“€μ–΄λ³΄λ©΄

//ES5
function hi(text) {
  text += 'ν•˜μ„Έμš”';
  return text;
}

//ES6
const hi = text => {
  text += 'ν•˜μ„Έμš”';
  return text
};

ν•¨μˆ˜κ°€ μ‹€ν–‰ λ‚΄μš©μ΄ λ”±νžˆ 없이 λ¦¬ν„΄λ§Œ ν•œλ‹€λ©΄ {μ€‘κ΄„ν˜Έ} μƒλž΅μ΄ κ°€λŠ₯ν•˜λ‹€.

//ES5
function getName(name) {
  return name;
}
 
//ES6
const hi = name => { return name };
const hi = name => name;

μœ„μ—μ„œ λ³΄λŠ” κ²ƒμ²˜λŸΌ {μ€‘κ΄„ν˜Έ}와 리턴이 μƒλž΅λ  경우, arrow function λ’€μ—λŠ” 리턴될 "κ°’"만 μ“°λ©΄ λœλ‹€.

  • 🎯또 λ‹€λ₯Έ 예제
//ES5
function getFullName(first, familly) {
  return first + familly;
}

//ES6
const hi = (first, familly) => { return first + familly };
const hi = (first, familly) => first + familly;

2. Assignment


  • function ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ ν‘œν˜„λœ welcome ν•¨μˆ˜λ₯Ό, ν™”μ‚΄ν‘œ ν•¨μˆ˜ ν‘œν˜„μœΌλ‘œ λ°”κΎΈμž.
  • 이후, ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ ν‘œν˜„λœ handleBio ν•¨μˆ˜λ₯Ό, function ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ ν‘œν˜„λœ ν•¨μˆ˜λ‘œ λ°”κΎΈμž.
// 문제1
function welcome(name){
  return "μ•ˆλ…•ν•˜μ„Έμš”" + name
}

// 응닡: Arrow Function을 ν™œμš©ν•˜μ—¬ λ³€ν™˜
const welcome = (name) => "μ•ˆλ…•ν•˜μ„Έμš”" + name;

// 문제2
const handleBio = (nickname, bio) =>  {
  const user = {
    nickname : nickname,
    bio : bio,
  }
   return user; 
}

// 응닡: Named Function을 ν™œμš©ν•˜μ—¬ λ³€ν™˜
const handleBio = function(nickname, bio) {
  const user = {
    nickname : nickname,
    bio : bio,
  }
  return user;
}
profile
μ•ˆλ…•ν•˜μ„Έμš”, 지식을 κ³΅μœ ν•˜λŠ” κ³΅κ°„μž…λ‹ˆλ‹€.

0개의 λŒ“κΈ€