TIL

23/10/13 TIL _ ๋‹จ์ถ•์†์„ฑ๋ช… (property shorthand)

GABOJOK 2023. 10. 13. 17:11

 

 

๐Ÿ‘€ obj์˜ ์ž๋ฃŒ๋ฅผ ์ด๋Ÿฐ์‹์œผ๋กœ๋„ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

  1. ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋‹ด๋Š”๋‹ค. 
  2. ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์—  obj์„ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์— ์ „์— ๋งŒ๋“ค์–ด๋‘” ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ๋„ฃ์–ด์ค€๋‹ค.
  3. ์ƒˆ๋กœ๋งŒ๋“  obj.๋ณ€์ˆ˜์ด๋ฆ„ ์„ ์ถœ๋ ฅํ•˜๋ฉด ๋งˆ์น˜ ๊ฐ์ฒด์ฒ˜๋Ÿผ ํ•ด๋‹น ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค. 
let p = "pop";
let j = "jaze";
let music = { p, j };
console.log(music.p); //pop

์ด๋Ÿฐ ๋ฐฉ์‹์„ ์šฐ๋ฆฌ๋Š”  ๋‹จ์ถ•์†์„ฑ๋ช… ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. 

 

 

 

๐Ÿš— ๋‹จ์ถ•์†์„ฑ๋ช… (property shorthand)

 

  • ๋‹จ์ถ•์†์„ฑ๋ช…์€ ๋ณ€์ˆ˜๊ฐ€ ๋ฏธ๋ฆฌ ์ค€๋น„๋œ ๊ฒฝ์šฐ, ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ์ฒด์˜ ํ‚ค์™€ ๊ฐ’์„ ํ•œ๋ฒˆ์— ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. 
  • ๋ฌธ์ž์—ด, ํ•จ์ˆ˜, ๊ฐ์ฒด ๋“ฑ ์ž๋ฃŒํ˜•์— ์ƒ๊ด€ ์—†์ด ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž. 

let country = "korea";
let member = {};

let addFamily = function (age, name, rel) {
  this.member[rel] = { age, name }; //key value ์„ ์–ธ ๋ฐ ํ• ๋‹น
  console.log(this);
  //{
  //   country: 'korea',
  //   member: { friend: { age: 30, name: 'chaeWon' } },
  //   addFamily: [Function: addFamily],
  // }
};

let family = { country, member, addFamily };

family.addFamily(30, "chaeWon", "friend");

 

์กฐ๊ธˆ ๋ณต์žกํ•œ ์˜ˆ์‹œ๊ธด ํ•˜์ง€๋งŒ ํ•œ๋ฒˆ ๋œฏ์–ด๋ณด์ž.

 

์ผ๋‹จ ๋‹จ์ถ•์†์„ฑ๋ช…( property Shorthand ) ์ด ํ•œ๋ˆˆ์— ๋ณด์ด๋Š” ๋ถ€๋ถ„ ๋จผ์ € ์‚ดํŽด๋ณด์ž.

๋ณ€์ˆ˜ country๋ฅผ ์„ ์–ธํ•˜๊ณ , family๋ผ๋Š” obj์— ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์คŒ์œผ๋กœ key&value๊ฐ€ ๋“ค์–ด๊ฐ”๋‹ค.

 

๐Ÿ‘Œ

 

 

๋‹ค์Œ์œผ๋กœ addFamily๋ฅผ ์‚ดํŽด๋ณด์ž.

  • addFamily ํ•จ์ˆ˜๋Š” family ๋ผ๋Š” ์˜ค๋ธŒ์ ํŠธ ๋‚ด๋ถ€์— ์žˆ๋Š” ํ•จ์ˆ˜์ด๋‹ค. 
  • ์˜ค๋ธŒ์žญํŠธ ๋‚ด์˜ ํ•จ์ˆ˜ ์•ˆ์—์„œ this์˜ ์˜๋ฏธ๋Š” ๊ทธ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋œปํ•œ๋‹ค.  (console.log(this)๋กœ ๊ฒ€์ฆ)
  • ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด, family.member ํ‚ค์˜ ๊ฐ’์œผ๋กœ ๋˜๋‹ค๋ฅธ obj๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. 
  • ํ‚ค ์ด๋ฆ„์€ rel,  ๊ฐ’์€ name, age ์ธ obj๊ฐ€ ์ƒ๊ฒจ๋‚œ๋‹ค. 
  • ๋งŒ๋“  ๋‹ค์Œ this๋ฅผ ์ถœ๋ ฅํ–ˆ์Œ์œผ๋กœ, family๋ผ๋Š” obj๋Š”  ์ €๋ ‡๊ฒŒ ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์ ธ ์žˆ๋Š” ์ƒํ™ฉ์ด ๋œ๋‹ค. 

 

 

 

 

 

์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ

https://itstart-190126.tistory.com/entry/ES6%EC%9D%98-%ED%96%A5%EC%83%81%EB%90%9C-%EA%B0%9D%EC%B2%B4-%EB%AC%B8%EB%B2%95-%EC%95%8C%EC%95%84%EB%B3%B4%EA%B8%B0-%EB%8B%A8%EC%B6%95-%EC%86%8D%EC%84%B1%EB%AA%85

 

ES6์˜ ํ–ฅ์ƒ๋œ ๊ฐ์ฒด ๋ฌธ๋ฒ• ์•Œ์•„๋ณด๊ธฐ - ๋‹จ์ถ• ์†์„ฑ๋ช…, ์†์„ฑ ๊ณ„์‚ฐ๋ช…, ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น

โ— ES6๋ถ€ํ„ฐ ์ถ”๊ฐ€๋œ ๋‹จ์ถ• ์†์„ฑ๋ช…์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ• ES6์— ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ JavaScript ๊ธฐ๋Šฅ์ธ ๋‹จ์ถ• ์†์„ฑ๋ช…์„ ํ™œ์šฉํ•˜์—ฌ, ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด ์†์„ฑ์„ ๋ฆฌํ„ฐ๋Ÿด๋กœ ์ •์˜

itstart-190126.tistory.com

 

 

 

es6์—๋Š” ์ •๋ง ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์ด ๋“ค์–ด์žˆ๋Š”๋ฐ, ์•„์ง ๋‚ฎ์„ค์–ด์„œ ์ •๋ง ๋งŽ์ด ๋ด์•ผ๊ฒŸ๋‹ค..

 

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

23/10/15 TIL __ .call() / .apply() / .bind()  (1) 2023.10.15
23/10/14 TIL __ this ์˜ ๋ฐ”์ธ๋”ฉ  (0) 2023.10.14
23/10/12 TIL _ switch / break / continue  (0) 2023.10.12
23/10/11 TIL  (1) 2023.10.12
23/10/08 TIL _ ๊ฒŒ์‹œํŒ ๊ธ€๋ฒˆํ˜ธ  (1) 2023.10.08