๐ŸŸจ JavaScript/๊ฐœ๋…

[mdn, ๋“œ๋ฆผ์ฝ”๋”ฉ ์™ธ] ๊ฐ์ฒด(Object)์™€ ํ”„๋กœํผํ‹ฐ(property) (2) : ์ƒ์„ฑ/์ˆ˜์ •๊ณผ cloning

Zoeeey 2022. 5. 31. 12:29

https://sunshineyellow.tistory.com/17?category=1037372 

 

[์ฝ”๋”ฉ์•™๋งˆ ์™ธ] ๊ฐ์ฒด(Object)์™€ ํ”„๋กœํผํ‹ฐ(property)

๊ฐ์ฒด(Object) key์™€ value๋กœ ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. 1. ํ•จ์ˆ˜ํ‘œํ˜„์‹ const goodCoder = function(name, age, coding) { name : 'Noran', age : 20, coding : function(){ console.log('ํ™”์ดํŒ…!'); } } name : 'Noran',..

sunshineyellow.tistory.com

https://sunshineyellow.tistory.com/18?category=1037372 

 

[์ฝ”๋”ฉ์•™๋งˆ ์™ธ] ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด๊ณผ ๊ฐ์ฒด ์ ‘๊ทผ๋ฒ•, ๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ์ž ํ•จ์ˆ˜

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ์„ ์ƒ์„ฑํ•  ๋•Œ๋Š” ์ง๊ด€์ ์ด๊ณ  ๊ฐ„ํŽธํ•œ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•˜๊ณ , ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋Œ€๋Ÿ‰์ƒ์‚ฐํ•  ๋•Œ๋Š” ์ƒ์„ฑ์žํ•จ์ˆ˜๋ฅผ

sunshineyellow.tistory.com

 

์œ„ ๊ธ€์„ ๋ณด์ถฉํ•ด ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ์˜ ์ƒ์„ฑ๊ณผ ์ˆ˜์ •, ์‚ญ์ œ์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ๋‹ค.


์ƒ์„ฑ

ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ƒ์„ฑ

const noran = new Goodcoder('noran', 20);

ํด๋ž˜์Šค ์—†์ด ๊ฐ์ฒด ์ƒ์„ฑ

const noran = { name : 'noran', age : 20 };

์ˆ˜์ •

key ์ถ”๊ฐ€

noran.hasjob = true;

key ์‚ญ์ œ

delete noran.hasjob;

cloning

์•„๋ž˜์˜ ๊ฒฝ์šฐ coder1๊ณผ coder2์˜ value๊ฐ€ ๊ฐ™์ด ๋ฐ”๋€๋‹ค.

const coder1 = { name : 'noran', age : 20 };
const coder2 = coder1;
coder2.name = 'paran';

console.log(coder1); //'paran'

properties ๋ณต์‚ฌํ•˜๊ธฐ (์˜ˆ์ „๋ฒ„์ „)

const coder3 = {};
for (key in coder1) {
  coder3[key] = coder1[key];
}

console.log(coder3); //{name : 'noran', age : 20}

properties ๋ณต์‚ฌํ•˜๊ธฐ (์‹ ๋ฒ„์ „)

const coder4 = {};
Object.assign(coder4, coder1);

console.log(coder4); //{name : 'noran', age : 20}

์œ„๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์ •๋ฆฌ๋  ์ˆ˜ ์žˆ๋‹ค.

const coder4 = Object.assign({}, coder1);

์ถœ์ฒ˜ : ๋“œ๋ฆผ์ฝ”๋”ฉ https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko