๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/JS

[JavaScript] ํ™”์‚ดํ‘œํ•จ์ˆ˜ vs ์ผ๋ฐ˜ํ•จ์ˆ˜

by rindev 2021. 10. 9.

 

โœ… ์ผ๋ฐ˜ ํ•จ์ˆ˜

  • this๋Š” ๋งˆ์ง€๋ง‰์œผ๋กœ ์ž์‹ ์„ ํ’ˆ๊ณ ์žˆ๋Š” scope๋กœ ํ•ญ์ƒ ๋ณ€ํ•จ (โŒ์–ด๋””์„œ ์„ ์–ธํ–ˆ๋Š๋ƒโŒ โญ•์–ด๋””์„œ ํ˜ธ์ถœํ–ˆ๋Š๋ƒโญ•)
const example = {
	name: "world",
	speak: function() {
		function getName() {
			return `${this.name}`;
		}
		return `Hello ${getName()}`;
	}
};
example.speak(); // Hello

 

//์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด..
const example = {
	name: "world",
	speak: function() {
		//์—ฌ๊ธฐ๊นŒ์ง€๋Š” this์— ๊ฐ์ฒด example์ด bind๋ผ์žˆ์Œ
		const self = this; // self๋ฅผ ์„ ์–ธํ•ด์„œ ๊ฒฐํ•ฉ์‹œํ‚ด
		function getName() {
			return `${self.name}`;
		}
		return `Hello ${getName()}`;
	}
};
example.speak(); // Hello world

์ฝ”๋“œ ์ถœ์ฒ˜

 

โœ… ํ™”์‚ดํ‘œํ•จ์ˆ˜ (Arrow Function)

  • ES6์— ๋“ฑ์žฅ^^...ใ…Žใ…‹
  • ์ƒ์œ„ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ „์—ญ = window
  • () ⇒ {} ๋ชจ์–‘์„ ํ•˜๊ณ ์žˆ์Œ
  • self ์•ˆ์จ๋„ ๋จ (์ฒ˜์Œ ๋ฐ”์ธ๋”ฉ ๋œ ์Šค์ฝ”ํ”„ ์•ˆ์—์„œ ๊ฐ€๋ฆฌํ‚ค๋Š” this๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š์Œ)
//์œ„๋ž‘ ๊ฐ™์€ ์ฝ”๋“œ์ž„
const example = {
	name: "world",
	speak: function() {
		const getName = () => {
			return `${this.name}`; //this๋ฅผ ์ •์ (lexically)์œผ๋กœ ๊ฒฐํ•ฉ์‹œํ‚ด
		}
		return `Hello ${getName()};
	}
};
example.speak(); // Hello world

 

๐Ÿ˜จ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์“ธ ๋•Œ ์ฃผ์˜์ !

  • prototype ์†์„ฑ์ด ์—†์ŒโŒ
  • ์ƒ์„ฑ์ž์˜ new ํ‚ค์›Œ๋“œ์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์—†์ŒโŒ
  • ๋ฉ”์†Œ๋“œ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋จโŒ
  • addEventListener ํ•จ์ˆ˜์˜ callback ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋จโŒ (๋‚ด๋ถ€์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ)

 

๐Ÿค” ๊ทธ๋ฆฌ๊ณ  ๋˜ ๋‹ค๋ฅธ ์ฐจ์ด์ ์€?

  • ๊ธฐ์กด์˜ function์€ ํ•จ์ˆ˜์˜ ์ด๋ฆ„ ์ž์ฒด๋ฅผ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์ง€๋งŒ,
    ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๋ณ€์ˆ˜์— ๋‹ด์•„์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•จ (ํ•จ์ˆ˜ ์ž์ฒด์˜ ํ˜ธ์ด์ŠคํŒ… ๋ถˆ๊ฐ€)
function example() { console.log("function"); }
const example = () => { console.log("arrow function"); }
  • ๊ธฐ์กด์˜ ํ•จ์ˆ˜๋Š” arguments๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ›์•„์˜ค๋Š” ์ธ์ž๋“ค์„ ๋ฐฐ์—ด๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ–ˆ์œผ๋‚˜,
    ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ๋Š” argument ๋Œ€์‹  rest parameter ์‚ฌ์šฉ
๐Ÿ’ก arguments 
arguments ๊ฐ์ฒด๋Š” ํ•จ์ˆ˜์— ์ „๋‹ฌ๋˜๋Š” ์ธ์ˆ˜์˜ ๋ฐฐ์—ด ์ง‘ํ•ฉ. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•จ.
let args = Array.prototype.slice.call(arguments);
const funcName = (...arg) => { console.log(arg) } // [1, 2, 3]
funcName(1, 2, 3);

 

 

 

๐Ÿ˜ ๊ฐ™์ด ๋ณด๋ฉด ์ข‹์„์ง€๋„ ๋ชจ๋ฅด๋Š” ๊ธ€

[JavaScript] JS์˜ Implicit Return (์•”์‹œ์  ๋ฐ˜ํ™˜)

 

 

๐Ÿ‘€์ฐธ๊ณ  (๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค)

๋”๋ณด๊ธฐ

https://kim-solshar.tistory.com/57

 

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] arrow function๊ณผ this

ecmascript6๊ฐ€ ๋‚˜์˜จ์ง€๋Š” ํ•œ์ฐธ๋์ง€๋งŒ ์•„์ง ๋ธŒ๋ผ์šฐ์ €(ํŠนํžˆ ie)๋“ค์ด ์ด๋ฅผ ์™„๋ฒฝํžˆ ์ง€์›ํ•˜์ง€ ์•Š๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ˜„์žฌ๋„ ์—ฌ์ „ํžˆ es5์—์„œ es6+๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ณผ๋„๊ธฐ์ธ๋ฐ, ๋‹คํ–‰ํžˆ babel์˜ ๋„์›€์œผ๋กœ ์ง€๊ธˆ ๋‹น์žฅ es6

kim-solshar.tistory.com

 

https://medium.com/@shlee1353/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-arguments-arrow-function-ba97bb84854e

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ (arguments ๊ฐ์ฒด, arrow function ํ™”์‚ดํ‘œํ•จ์ˆ˜)

1. arguments

medium.com

 

https://bluayer.com/25

 

[JavaScript] Arrow function๊ณผ this ํ‚ค์›Œ๋“œ

์„œ๋ก  ES6์—๋Š” ๋‹ค์–‘ํ•œ ๋‚ด์šฉ๋“ค์ด ์ƒˆ๋กœ ํฌํ•จ๋˜์—ˆ๊ณ , ์œ ์šฉํ•œ ๊ฒƒ๋“ค์ด ๋งŽ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์‚ฌ์šฉ ๋นˆ๋„๋กœ ์ˆœ์œ„๋ฅผ ๋งค๊ธด๋‹ค๋ฉด, ํ•„์ž๋Š” ์˜ค๋Š˜ ๋‹ค๋ฃฐ Arrow function์ด ๋‹ค์„ฏ ์†๊ฐ€๋ฝ ์•ˆ์— ๊ผฝํž ๊ฒƒ์ด๋ผ๊ณ  ํ™•์‹ ํ•œ๋‹ค. ๋˜ํ•œ, Ar

bluayer.com

 

https://helloinyong.tistory.com/74

 

[2019.04.17] ES6 ๋ฌธ๋ฒ• - arrow Function๊ณผ ์ผ๋ฐ˜ function์˜ ์ฐจ์ด

S6์—์„œ arrowFunction ๋ฐฉ์‹์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ , ๊ทธ๋ƒฅ ์“ฐ๊ฒŒ ๋˜๋ฉด ๋œ ํ†ต ๋‹นํ•œ๋‹ค... ๋˜‘๊ฐ™์ด ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐฉ์‹์ด๊ธด ํ•˜์ง€๋งŒ ๋™์ž‘ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ๋‹ค๋ฅธ ํ•œ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๊ณต๋ถ€ํ•ด๋„ ์ดํ•ดํ•˜๊ธฐ

helloinyong.tistory.com

 

https://velog.io/@modolee/javascript-function-vs-arrow-function

 

JavaScript - Function vs Arrow Function

ES6 ๋ถ€ํ„ฐ ์ถ”๊ฐ€ ๋œ Arrow Function์™€ ๊ธฐ์กด์˜ ์ผ๋ฐ˜ Function์„ ๋น„๊ต ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ํŠน์ง•. ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ. ํ˜ธ์ด์ŠคํŒ…

velog.io

 

 

๋Œ“๊ธ€