-
Arrow FunctionTIL/ES6+ 2020. 12. 3. 12:29
π Arrow Function
Arrow Function, νμ΄ν ν¨μλ ES6μμ λ°νλ ννμ ν¨μμ΄λ€. μ΄λ¦ κ·Έλλ‘ νμ΄ν
=>
λͺ¨μμ μ¬μ©ν ν¨μλ‘, μ°λ¦¬κ° νμμfunction
ν€μλλ₯Ό μ΄μ©ν΄ ν¨μλ₯Ό μ¬μ©νλ κ²μ λΉν΄ μ§§κ³ κΉλν μ½λλ₯Ό μμ±ν μ μλ κ²μ΄ νΉμ§μ΄λ€. κ·ΈλΌ ES5μμ μΌλ ν¨μ ννμ ES6μ νμ΄ν ν¨μ ννλ₯Ό λΉκ΅ν΄λ³΄μ!// ES5 function helpMe() { console.log('Obi-Wan Kenobi'); } // or var helpMe = function() { console.log('Obi-Wan Kenobi'); } // ES6 const helpMe = () => { console.log('Obi-Wan Kenobi'); }
νμ΄νν¨μλ μ΄λ κ² λ³μμ
()
μ=>
λ₯Ό μ΄μ©ν΄ ν¨μλ₯Ό μ μ₯νλ€.λ¬Έλ²
function
ν€μλλ₯Ό μ΄μ©ν΄ μμ±ν ν¨μλ₯Ό νμ΄ν ν¨μλ‘ λ°κΎΈμ΄λ³΄λ©΄// ES5 function helpMe(from) { console.log('Obi-Wan Kenobi'); console.log(`message from ${from}`); } // νμ΄ν ν¨μλ‘ λ°κΎΈκΈ° 1. function ν€μλλ₯Ό μμ€λ€ helpMe(from) { console.log('Obi-Wan Kenobi'); console.log(`message from ${from}`); } 2. ν¨μ μ΄λ¦μΌλ‘ λ³μλ₯Ό μ μΈνκ³ = μ μ°λ³μ ν¨μμ 맀κ°λ³μλ€μ κ΄νΈλ‘ λ¬Άλ κ²κ³Ό ν¨μ λͺΈν΅μ μ λλ€ const helpMe = (from) { console.log('Obi-Wan Kenobi'); console.log(`message from ${from}`); } 3. 맀κ°λ³μλ€κ³Ό ν¨μ λͺΈν΅ μ¬μ΄μ => μ°μ°μλ₯Ό λ£μ΄μ€λ€ const helpMe = (from) => { console.log('Obi-Wan Kenobi'); console.log(`message from ${from}`); } 4. νΈμΆ! helpMe('Princess Leia');
μ΄λ μ£Όμν μ μ
- μΈμκ° ν κ°λ°μ μλ€λ©΄ κ΄νΈλ₯Ό μλ΅ν΄λ λλ€. νμ§λ§ μΈμκ° μκ±°λ 2κ° μ΄μμΌ λλ λ°λμ κ΄νΈλ₯Ό μ¬μ©νλ€.
- ν¨μμ λͺΈν΅ λΆλΆμμ μ°μ° μ²λ¦¬λ₯Ό νλ λ¬Έμ₯ μμ΄ returnλ§ μν νλ€λ©΄ κ°μΈλ
{}
λ₯Ό μλ΅ν μ μλ€. (implicit return)
// ex. const whoAreU = () => "I'm your father."; // ν¨μλ₯Ό νΈμΆνλ©΄ I'm your father.λ₯Ό 리ν΄ν©λλ€
λ§μ½
{}
λ₯Ό μλ΅νμ§ μκ³ μ¬μ©νλ€λ©΄ 리ν΄ν κ°μ λ°λμ return λͺ λ Ήμ΄λ₯Ό μ μ΄μ€μΌνλ€.const whoAreU = () => { "I'm your father." }; // π¨ ERROR const whoAreU = () => { return "I'm your father." }; // π
π thisμ νμ΄ν ν¨μ
ν¨μλ₯Ό μ¬μ©ν λ
this
λ μ°Έ μ μ©νκ² μ°μΈλ€. μλ₯Ό λ€μ΄addEventListner
ν¨μ μμμthis
λ κ·Έ μ΄λ²€νΈκ° κ±Έλ €μλ μμλ₯Ό κ°λ¦¬ν€κ² λλ€. νμ§λ§ μ¬νκ²λ νμ΄ν ν¨μμμλthis
λ₯Ό μ¬μ©ν μ μλ€. λ§μ½ νμ΄ν ν¨μμμthis
λ₯Ό νΈμΆνλ©΄ ν΄λΉ κ°μ²΄κ° μλ κ·Έ κ°μ²΄μ μμ μ€μ½νλ₯Ό κ°λ¦¬ν¨λ€.βμμ?
βοΈμΌλ°μ μΌλ‘function
μ μ¬μ©ν ν¨μλ ν¨μλ₯Ό μ μΈν λthis
κ° κ°λ¦¬ν¬ κ°μ²΄κ° λμ μΌλ‘ κ²°μ λμ§λ§ νμ΄ν ν¨μλ μ μ μΌλ‘ κ²°μ λ©λλ€. κ·Έλμ μΈμ λ μμ μ€μ½νμthis
λ₯Ό κ°λ¦¬ν€κ² λμ£ . λͺ κ°μ§ μμμ ν¨κ» λ΄ μλ€!1. μ μμμ μ μΈν ν¨μ const callThis = () => { console.log(this) } // window 2. Object μμ ν¨μ const jedi = { one: 'Luke', two: 'Rey', callThis: () => { console.log(this); } } // jedi κ°μ²΄κ° λμ€κΈΈ λ°λ¬μ§λ§... window 3. addEventListner callback button.addEventListner('click', () => { console.log(this); // buttonμ΄ λμ€κΈΈ λ°λ¬μ§λ§... window }); 3. prototypeμ ν λΉ function jedi(name) { this.name = name; } jedi.prototype.callThis = () => { console.log(this); } let rey = new jedi('Rey'); rey.callThis(); // jediκ° λμ€κΈΈ λ°λ¬μ§λ§... window
μ΄λ° κ²½μ°λ€μλ νμ΄ν ν¨μκ° μλ μΌλ° ν¨μλ₯Ό μ¬μ©ν΄μΌνλ€.
+) νμ΄ν ν¨μμ μ¦μμ€νν¨μ
((from) => console.log(`message from ${from}`))('Princess Leia');
'TIL > ES6+' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Default Value & Destructuring (0) 2021.01.02 Array Function2 (0) 2020.12.10 Array Function (0) 2020.12.07 For Loop (0) 2020.12.05 let & const (0) 2020.12.01