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

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

by rindev 2021. 5. 23.

ES6๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ๋„์ž…ํ–ˆ๋‹ค. 

 

๐Ÿคท‍โ™€๏ธ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ ค๋ฉด?

{foodILike.map(function (dish) {
	return (
		<Food name={dish.name} picture={dish.image} rating={dish.rating} />
	);
})} 
      

{foodILike.map((dish) => (
	<Food name={dish.name} picture={dish.image} rating={dish.rating} />
))}

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ฐ”๊พธ๋ ค๋ฉด function ์˜ˆ์•ฝ์–ด๋ฅผ ์ง€์šฐ๊ณ  => ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

์ธ์ž๊ฐ€ ํ•˜๋‚˜์ผ ๊ฒฝ์šฐ์—๋Š” ๊ด„ํ˜ธ (dish๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๊ด„ํ˜ธ)๋ฅผ ์“ฐ์ง€ ์•Š์•„๋„ ๋˜์ง€๋งŒ... ์ด๊ฑด ๊ฐœ์ธ์˜ ์ฝ”๋”ฉ ์Šคํƒ€์ผ์ด๋ผ๊ณ  ํ•œ๋‹ค.

๊ทผ๋ฐ VSC๋Š” ์ž๋™์œผ๋กœ ๋ถ™์—ฌ์ฃผ๋˜๋ฐ ๋ชจ๋ฅด๊ฒ ๋‹ค ์ข€๋” ์จ๋ด์•ผ ์•Œ๋“ฏ^^;

 

๊ทธ๋ฆฌ๊ณ  ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์•”์‹œ์  ๋ฐ˜ํ™˜์„ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ทธ๋ž˜์„œ return ์˜ˆ์•ฝ์–ด๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Œ

 

๐Ÿคท‍โ™€๏ธ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ ๊ด„ํ˜ธ๋Š”...?

  sub = () => {
    this.setState((current) => ({ count: current.count - 1 }));
    //this.setState( current => {return current.count += 1});
  };

์ฃผ์„์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๋ถ€๋ถ„์€ returnํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์— ( )๊ฐ€ ์žˆ๊ณ ,

๋ฐ‘์—๋Š” ๊ด„ํ˜ธ ์—†์ด ๋ฐ”๋กœ { }๊ฐ€ ๋ฆฌํ„ด๋˜๊ณ  ์žˆ๋‹ค! ๋‚˜๋งŒ ํ—ท๊ฐˆ๋ฆฐ๊ฒŒ ์•„๋‹ˆ๊ณ  ์งˆ๋ฌธ์„ ํ•ด๋‘” ์‚ฌ๋žŒ์ด ์žˆ์–ด์„œ ๋‹ต๋ณ€์„ ์ฐธ๊ณ ํ•ด์„œ ์ •๋ฆฌํ•œ๋‹ค.

 

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒฝ์šฐ ( ) => { } ํ˜•์‹์„ ๊ฐ€์ง€๋Š”๋ฐ, { } ์ด ๋ถ€๋ถ„์ด returnํ•  ๋Œ€์ƒ์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

๊ทผ๋ฐ ์œ—์ค„์€ ๋ณด๋ฉด { count : current.count - 1 } ์ด๊ฒŒ ๊ฐ์ฒด์ธ์ง€, ์•„๋‹ˆ๋ฉด ๋ฆฌํ„ด์„ ํ•ด์•ผํ•˜๋Š”๊ฑด์ง€ ํ™”์‚ดํ‘œ๊ฐ€ ํ—ท๊ฐˆ๋ฆฌ๋Š”๊ฑฐ๋‹ค!

๊ทธ๋ž˜์„œ ์ด๊ฑธ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ( { } ) ์ด๋ ‡๊ฒŒ ๊ฐ์‹ธ์„œ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

์ฃผ์„์ฒ˜๋ฆฌ ๋œ ๋ถ€๋ถ„์—์„œ๋Š” { }์•ˆ์— return์ด ์žˆ์œผ๋‹ˆ ์ž˜ ๋ฆฌํ„ด์ด ๋œ ๊ฒƒ์ด๋‹ค!

 

์•„๊ทธ๋ฆฌ๊ณ  ์ฐพ๋‹ค๊ฐ€ ๋˜ ๋ฐœ๊ฒฌํ•œ๊ฑด๋ฐ,

์•”์‹œ์  ๋ฐ˜ํ™˜์„ ์‚ฌ์šฉํ•  ๋•Œ, ์ค‘๊ด„ํ˜ธ๊ฐ€ ํ•จ์ˆ˜ ๋ณธ๋ฌธ์„ ์—ฌ๋Š” ๊ฒƒ์œผ๋กœ ์˜ค์ธ๋˜์ง€ ์•Š๋„๋ก ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค <-๋ผ๋Š” ๊ธ€๋„ ๋ด„

์˜์–ด์—ฌ์„œ ๊ตฌ๊ธ€์ด ์ œ๋Œ€๋กœ ๋ฒˆ์—ญ์„ ํ–ˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค๐Ÿค”

 

 

๐Ÿคท‍โ™€๏ธ ์ธ์ž ์—†์ด๋„ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š”๋ฐ?

const example = names.map( () => `wow` );

์ธ์ž ์—†์ด ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ทธ๋ƒฅ ๋นˆ ๊ด„ํ˜ธ๋กœ ๋‘ฌ๋„ ๋œ๋‹ค.

๋นˆ ๊ด„ํ˜ธ๊ฐ€ ๋ง˜์— ์•ˆ๋“ค๋ฉด ์•„๋ฌด๊ฑฐ๋‚˜ ์จ๋„ ๋œ๋‹ค. ์ด๋Ÿฐ๊ฑธ ์‚ฌ์šฉ ํ›„ ๋ฒ„๋ฆฌ๋Š” ๋ณ€์ˆ˜(throwaway variable)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค...๐Ÿฅบ

 

 

 

๋ฆฌ์•กํŠธ ํ•˜๋ฉด์„œ ๋งŒ๋‚œ ์นœ๊ตฐ๋ฐ ์ต์ˆ™ํ•ด์ง€๋ ค๋ฉด ๋งŽ์ด ์จ๋ด์•ผ๊ฒ ๋‹ค๐Ÿ˜‚

 

 

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

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

 

 

์ฐธ๊ณ )

https://ui.toast.com/weekly-pick/ko_20160912

์ธ๊ฐ•์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€๋“ค 

 

'๐Ÿ’๊ณต๋ถ€ > JS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] IIFE(Immediately Invoked Function Expression)  (0) 2021.09.15
[JavaScript] ์ด๋ฒคํŠธ ๋ฃจํ”„  (0) 2021.08.14
[JavaScript] Arrow function vs Function  (0) 2021.07.28
[JavaScript] innerText vs innerHTML  (0) 2020.11.06
[JavaScript] image ๊ฐ์ฒด  (0) 2020.11.06

๋Œ“๊ธ€