๐ ES6 = ECMA Script 2015
ES๋ ECMA Script์ ์ค์๋ง์ ๋๋ค. ECMA Script๋ JavaScript๋ฅผ ํ์คํ ์ํค๋ ค๊ณ ํ์ํ์ต๋๋ค.(ECMAScript (or ES) is a scripting-language specification standardized by Ecma International)
๐งโโ๏ธ arrow function//ES5 function() {} //ES6 () => {}
์ด๋ฆ์ด ์๋ ํจ์์ ๊ธฐ๋ณธ์ ์ธ ํํ์ ๋๋ค.
ES6์์๋ function์ด๋ผ๋ ํค์๋๊ฐ ๋น ์ง๊ณ ์๊ดํธ๋ง ๋จ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ=>
(arrow) ๊ฐ ์ถ๊ฐ๋์์ต๋๋ค.
์ด๋ฆ์ด ์๋ ํจ์๋ฅผ ๋ง๋ค ๋๋ ์๋์ ๊ฐ์ต๋๋ค.//ES5 function getName() {} //ES6 const getName = () => {}
getName() //ํธ์ถํ ๋. ๋ ๋ค ๊ฐ์
์ฌ์ค function(ํจ์)๋ ๋ณ์์ ์ ์ฅํ ์ ์๋ ํ๋์ ์์ ๋๋ค.
๊ทธ๋์ 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) => {} //์ธ์๊ฐ ๋ ๊ฐ์ธ ๊ฒฝ์ฐ
์ด์ returnํ๋ ํจ์๋ฅผ ๋ณผ๊น์?
//ES5 function hi(text) { text += 'ํ์ธ์'; return text; } //ES6 const hi = text => { text += 'ํ์ธ์'; return text };
๋ง์ฝ ํจ์๊ฐ ์คํ๋ด์ฉ์ด ๋ฑํ ์์ด return๋ง ํ๋ค๋ฉด return ํค์๋์ ์ค๊ดํธ๊ฐ ์๋ต๊ฐ๋ฅํฉ๋๋ค.
//ES5 function getName(name) { return name; } //ES6 const hi = name => { return name }; const hi = name => name; //์ค๊ดํธ ์๋ต ๊ฐ๋ฅ
์ค๊ดํธ์ return๋ฌธ์ด ์๋ต๋ ๊ฒฝ์ฐ, ํ์ดํ ์ค๋ฅธ์ชฝ์๋ ๋ฆฌํด๋ "๊ฐ"๋ง ์ฐ์ฌ์ผ ํฉ๋๋ค.
๋ค๋ฅธ ์ฝ๋๊ฐ ๋ค์ด๊ฐ๋ฉด ์๋ฉ๋๋ค.//ES5 function getFullName(first, family) { return first + family; } //ES6 const hi = (first, family) => { return first + family }; const hi = (first, family) => first + family; //๋ฆฌํด ์๋ต, ๋ฆฌํด ๋ ๊ฐ๋ง ์จ์ผ ํจ!
๐งโโ๏ธ Assignment
์ด๋ฒ assignment๋ ์๋์ฑ์ ์ด ์๋๋๋ค. ์์ฑํด์ ์ ์ถํ๋ฉด ์ฑ์ ํด๋๋ฆฌ๊ฒ ์ต๋๋ค.
1. ์ผ์ชฝ ์ฝ๋์ฐฝ์์ function์ ์ฌ์ฉํ ํจ์ํํ๋ฒ์ ES6 ํํ์ ๋ฐ๊ฟ์ฃผ์ธ์.
2. ์ผ์ชฝ ์ฝ๋์ฐฝ์์ ES6์ ํจ์ ํํ์ function ํค์๋๋ฅผ ์ฌ์ฉํด์ ๋ฐ๊ฟ์ฃผ์ธ์.// function ์ฌ์ฉ function welcome(name){ return "์๋ ํ์ธ์" + name } // ์๋์์ ๋ณํํด์ฃผ์ธ์! const welcome = name => { return "์๋ ํ์ธ์" + name } welcome("์ง์์ด"); // ES6 const handleBio = (nickname, bio) => { const user = { nickname : nickname, bio : bio, } return user; } // ์๋์์ ๋ณํํด์ฃผ์ธ์! function handleBio(nickname, bio) { const user = { nickname : nickname, bio : bio, } return user; }