πŸ’‘ κΈ°μ‘΄ν•¨μˆ˜ vs ν™”μ‚΄ν‘œ ν•¨μˆ˜

κΈ°μ‘΄ μžλ°”μŠ€ν¬λ¦½νŠΈ ν•¨μˆ˜

// ν•¨μˆ˜ν‘œν˜„μ‹
let test = function(){console.log("μ•ˆλ…•")};	// μ•ˆλ…•

// ν™”μ‚΄ν‘œ ν•¨μˆ˜
let test = () => console.log("μ•ˆλ…•");	// μ•ˆλ…•

ν™”μ‚΄ν‘œ ν•¨μˆ˜ 

ν™”μ‚΄ν‘œ ν•¨μˆ˜λŠ” 항상 읡λͺ… ν•¨μˆ˜μ΄λ‹€.

// λ§€κ°œλ³€μˆ˜ X
let test = () => console.log('μ•ˆλ…•');
console.log(test);
test();

//	() => console.log('μ•ˆλ…•')
//	μ•ˆλ…•

// λ§€κ°œλ³€μˆ˜ 1개
let test = x => x;
test(1);
// 1

// λ§€κ°œλ³€μˆ˜ 2개
let test =(a,b) => {return a+b};
test(1,2)
// 3

// return이 μ—†λŠ” 경우
let test = (a,b) => {a+b};
test(1,2);
// undefined

// μ—¬λŸ¬μ€„μ„ μž‘μ„±ν•΄μ•Όν•˜λŠ” 경우
let test = (a,b) => {
	let c = 3;
	return a+b+c;
    }
test(1,2);
// 6


{}λ₯Ό μ‚¬μš©ν•˜λŠ” 경우 return을 κΌ­ μ‚¬μš©ν•΄μ•Όν•œλ‹€.

{}λŠ” μ½”λ“œλ₯Ό μ—¬λŸ¬μ€„ μž‘μ„±ν•΄μ•Όν•  λ•Œ μ‚¬μš©ν•œλ‹€.


πŸ’‘ κΈ°λ³Έκ°’ λ§€κ°œλ³€μˆ˜ (default function paramerter)

κΈ°μ‘΄ μžλ°”μŠ€ν¬λ¦½νŠΈ

function multi(a,b)
{
    b = (typeof b != 'undefined') ? b : 1;
    return a * b
}

console.log(multi(5,2));	// 10
console.log(multi(5));		// 5

 

ES6

// ES2015(ES6)
function multi(a,b=1)
{
	return a * b;
}

console.log(multi(5,2));	// 10
console.log(multi(5));		// 5

- κΈ°μ‘΄μ—λŠ” λ””ν΄νŠΈλ₯Ό μ§€μ •ν• μˆ˜ μ—†μ–΄ μ½”λ“œμ•ˆμ—μ„œ 섀정을 ν•΄μ•Ό ν–ˆμ§€λ§Œ ES6μ—μ„œλŠ” λ°”λ‘œ μ„€μ •ν• μˆ˜ 있게 λ˜μ—ˆλ‹€.

 

 

++ this λΆ€λΆ„ μΆ”κ°€ν•˜κΈ°

 

μ°Έκ³ 

velog.io/@ki_blank/JavaScript-%ED%99%94%EC%82%B4%ED%91%9C-%ED%95%A8%EC%88%98Arrow-function

 

JavaScript - ν™”μ‚΄ν‘œ ν•¨μˆ˜(Arrow function)

ν™”μ‚΄ν‘œ ν•¨μˆ˜λŠ” ES6λ¬Έλ²•μž…λ‹ˆλ‹€. function ν‚€μ›Œλ“œ μ‚¬μš©ν•΄μ„œ ν•¨μˆ˜λ₯Ό λ§Œλ“  것보닀 κ°„λ‹¨νžˆ ν•¨μˆ˜λ₯Ό ν‘œν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€. ν™”μ‚΄ν‘œ ν•¨μˆ˜λŠ” 항상 읡λͺ…μž…λ‹ˆλ‹€. 1. ν™”μ‚΄ν‘œ ν•¨μˆ˜μ˜ 기본문법 콜백 ν•¨μˆ˜μ—μ„œλ„ μ‚¬μš©

velog.io

ko.javascript.info/arrow-functions-basics

 

ν™”μ‚΄ν‘œ ν•¨μˆ˜ κΈ°λ³Έ

 

ko.javascript.info

jeong-pro.tistory.com/110

 

ES6 ν™”μ‚΄ν‘œ ν•¨μˆ˜(arrow function) 변경점 μš”μ•½ (μ‚¬μš©λ²•, thisλ“±)

Arrow function 1. μ‚¬μš©λ²• 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 //기쑴의 function sample var double = function(x){     return x*2; } //arrow function sample const double =..

jeong-pro.tistory.com

 

+ Recent posts