When โnotโ to use arrow function
์๋์ ๋ด์ฉ์ http://rainsoft.io/when-not-to-use-arrow-functions-in-javascript/ ๋ฅผ ๋ฐํ์ผ๋ก arrow function์ ์ฌ์ฉ ์ ์ฃผ์ ํ๊ธฐ์ํด ๋ฒ์ญ์ ํ ๋ด์ฉ์
๋๋ค.ํฅํ ์๋ฌธ์ ๋ธ๋ก๊ทธ ์ฌ์ฉ์์ ์์ฒญ์ผ๋ก ๋ฌธ์ ๊ฐ ์๊ธด๋ค๋ฉด ํฌ์คํธ ํ๊ฒ์ ์ง์ฐ๋๋ก ํ๊ฒ ์ต๋๋ค.๋ํ sns์๋ ๊ณต์ ๋ ํ์ง ์์์ต๋๋ค. ์ด์ ํ์ธ ํ์๊ณ ์ ๋ ๋ฐ๋ก ๋ณต์ฌํ์ฌ ๋ฐฐํฌ๋ ํ์ง ์์ผ์
จ์ผ๋ฉด ํฉ๋๋ค.
ECMAScript 6(ECMA2015)์์ ํ์ค์ผ๋ก ์ ํด์ง arrow function์ ์ฌ์ฉํ์ง ๋ง์์ผ ํ๊ฑฐ๋ ๋๋ ์ฌ์ฉํ๋ฉด ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๋ ๋ช๊ฐ์ง ์๋ฅผ ์์ ๋ณด๋๋ก ํ์
์๋์ ์์ค ์ฝ๋์ ๊ฐ์ด Object literal ๋ฐฉ์์์ property๋ฅผ ํจ์ ํํ๋ก ์ ์ ์ arrow function์ผ๋ก ์ ์ ํ๊ธฐ๋ฐ ๋๋ฉด arrow function์ this๊ฐ ํด๋น Object๊ฐ ์๋ window๊ฐ ๋์ด ์ํ๋ ๊ฒฐ๊ณผ๊ฐ ๋์ค์ง ์์ ์ ์๋ค.
var calculate = { array: [1, 2, 3], sum: () => { console.log(this === window); // => true return this.array.reduce((result, item) => result + item); } }; console.log(this === window); // => true // Throws "TypeError: Cannot read property 'reduce' of undefined" calculate.sum();
์์ ์์ค์ฝ๋๋ฅผ ๋ณด๋ฉด this์ window๋ ๊ฐ๋ค๋ ๊ฒฐ๊ณผ๊ฐ ๋์์ผ๋ฉฐ, calculate.sum()์ TypeError๋ฅผ ๋ฐ์ํ๋ค.์์ ๊ฐ์ ๊ฒ์ function expression ๋๋ shorthand syntax๋ฅผ ์ฌ์ฉํ์ฌ ํด๊ฒฐ์ ํ ์ ์๋ค.
var calculate = { array: [1, 2, 3], sum() { console.log(this === calculate); // => true return this.array.reduce((result, item) => result + item); } }; calculate.sum(); // => 6
์์ ๊ฐ์ด calculate์ this๋ ๊ฐ์ ๊ฒ์ ๋์ด calculate.sum()๋ ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ฒ ๋๋ค.
Object์ prototype๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์๋ ๋ฐ์์ ํ๋ค. ์๋์ ๊ฐ์ด arrow function์ ์ฌ์ฉํ๋ฉฐ this๋ window์ ๊ฐ๊ฒ ๋๋ฉฐ, ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ง ๋ชปํ๊ฒ ๋๋ค.
function MyCat(name) { this.catName = name; } MyCat.prototype.sayCatName = () => { console.log(this === window); // => true return this.catName; }; var cat = new MyCat('Mew'); cat.sayCatName(); // => undefined
์์ ๊ฐ์ ์์ค์ฝ๋๋ shorthand syntax๋ก๋ ์์ ์ด ๋ถ๊ฐ๋ฅ ํ๋ฉฐ, function expression(ํจ์ํํ์)์ผ๋ก ์์ ํ๋ฉด ๋๋ค.
function MyCat(name) { this.catName = name; } MyCat.prototype.sayCatName = function() { console.log(this === cat); // => true return this.catName; }; var cat = new MyCat('Mew'); cat.sayCatName(); // => 'Mew'
2. Callback functions with dynamic context
arrow function์ dynamic context์ธ ๊ฒฝ์ฐ์๋ ์ฌ์ฉ์ด ๋ถ๊ฐ๋ฅ ํ๋ค. this๋ก ์ธํด ๊ฐ์ฒด์ ์ ๊ทผ์ด ์๋๊ธฐ ๋๋ฌธ์ด๋ค. ์๋์ ์์ค์ฝ๋๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋ ์คํํ๋ callback function๋ฅผ arrow function์ผ๋ก ์์ฑ์ ํ๋ค. ์ด๋ ๊ฒ ๋๋ฉด this๊ฐ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํจ ํ์ผ์ด ๋์ง ์๊ธฐ ๋๋ฌธ์ ์ด๋ฒคํธ ๋ฐ์์ ํจ์๊ฐ ์ ๋๋ก ์คํ์ด ๋์ง ์๋๋ค.
var button = document.getElementById('myButton'); button.addEventListener('click', () => { console.log(this === window); // => true this.innerHTML = 'Clicked button'; });
์ด๊ฒ์ ์๋์ ๊ฐ์ด function expression(ํจ์ํํ์)์ผ๋ก ๋ณ๊ฒฝํ์ฌ์ผ ์คํ์ด๋๊ณ this๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํจ target์ด ๋๋ค.
var button = document.getElementById('myButton'); button.addEventListener('click', function() { console.log(this === button); // => true this.innerHTML = 'Clicked button'; });
์์ฑ์ ํจ์์์๋ arrow function ์ typeError๋ฅผ ๋ฐ์ํ๋ค. ์์ค์ฝ๋๋ฅผ ๋ณด๋ฉด TypeError๊ฐ ๋ฐ์๋๋ค.
var Message = (text) => { this.text = text; }; // Throws "TypeError: Message is not a constructor" var helloMessage = new Message('Hello World!');
์ด๊ฒ์ ํด๊ฒฐํ๊ธฐ ์ํ์๋ function expression์ ์ฌ์ฉํ์ฌ ํด๊ฒฐ ํ ์ ์๋ค.
var Message = function(text) { this.text = text; }; var helloMessage = new Message('Hello World!'); console.log(helloMessage.text); // => 'Hello World!'
๋ง์ง๋ง์ผ๋ก ๋๋ฌด ๋ง์ arrow function์ ์ฌ์ฉ์ผ๋ก ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๋ ๊ฒฝ์ฐ์ด๋ค.์๋์ ์์ค์ฝ๋๋ ์ฌํํ๊ณ ๊ฐ๋จํ์ง๋ง ์ดํดํ๋๋ฐ ์ฝ๊ฐ์ ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค.๊ทธ๊ฒ์ curly braces์ผ๋ก ์์ฑ๋์๋ค.
let multiply = (a, b) => b === undefined ? b => a * b : a * b; let double = multiply(2); double(3); // => 6 multiply(2, 3); // => 6
์ด๋ฌํ ๊ฒฝ์ฐ๋ ์ฌํํ ๊ฒ ๋ณด๋ค ํ์
์ ์ํด ์ฝ๋๋ฅผ ์ดํดํ๊ธฐ ์ฝ๋๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์ข๋ค.
function multiply(a, b) { if (b === undefined) { return function(b) { return a * b; } } return a * b; } let double = multiply(2); double(3); // => 6 multiply(2, 3); // => 6
์์ ์์ค์ฝ๋๋ ์ฅํฉํ๊ณ ์งง์ ์์ค์ฝ๋๋ฅผ ๊ท ํ์ด ๋ง๊ฒ ์์ ํ ๊ฒ์ด๋ค.
arrow function์ ์์ฌํ ์ฌ์ง๊ฐ ์์ด ์ข์ ๊ฒ์ด๋ค. arrow fuction ๋จ์ํ๋ฉด์๋ ์ ํํ ๊ณณ์ ๊ฐ์ ธ๋ค ์จ์ผ ํ๋ค. ๋ช๋ช ์ํฉ์ ์ด์ต์ ๋ค๋ฅธ ๊ณณ์ ๋ถ์ด์ต์ ๊ฐ์ ธ๋ค ์ค๋ค. ์์ ์ธ๊ธํ ๊ณณ์์๋ arrow function์ ์ฌ์ฉํ์ง ๋ง์์ผ ํ๋ค.