๐Ÿ“’ Javascript ๋ฐ์ดํ„ฐ #02 :)

zooyahoยท2022๋…„ 3์›” 31์ผ
0

study with me

๋ชฉ๋ก ๋ณด๊ธฐ
4/19
post-thumbnail

4. ๊ฐ์ฒด(Object)

๐Ÿ“Œ ์ •์  ๋ฉ”์†Œ๋“œ(Static)

: Object๊ฐ์ฒด์˜ prototype ์†์„ฑ์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹Œ ๋ฉ”์†Œ๋“œ
: ์ง์ ‘์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๋ฉ”์†Œ๋“œ!!!!
ex) {}.assign() -> ์‚ฌ์šฉ ๋ชปํ•จ

๐Ÿ”ต Object.assign()

  • ์ถœ์ฒ˜ ๊ฐ์ฒด๋“ค์˜ ๋ชจ๋“  ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ์ž์ฒด ์†์„ฑ์„ ๋ณต์‚ฌํ•ด ๋Œ€์ƒ ๊ฐ์ฒด์— ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค. ๊ทธ ํ›„ ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ.
  • assign(๋Œ€์ƒ๊ฐ์ฒด, ํ•˜๋‚˜ ์ด์ƒ์˜ ์ถœ์ฒ˜ ๊ฐ์ฒด)
    : ๋ฐ˜ํ™˜๊ฐ’ -> ๋Œ€์ƒ๊ฐ์ฒด!!!!

๐Ÿ‘พ #1

const target = { a:1, b:2 };
const source = { b: 4, c:5 };
const returnTarget = Object.assign(target, source);

console.log(target);
// Object { a: 1, b: 4, c: 5 }
console.log(returnTarget);
// Object { a: 1, b: 4, c: 5 }
console.log(target === returnTarget); // true

assign({}, ํ•˜๋‚˜ ์ด์ƒ์˜ ์ถœ์ฒ˜ ๊ฐ์ฒด)
: ๋น„์–ด์žˆ๋Š” ๊ฐ์ฒด์— ๋‘๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์‚ฝ์ž…ํ•ด, ๋‘๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ํ•ฉ์นœ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ฌ!!

๐Ÿ‘พ #2

const source1 = { a:1, b:2 };
const source2 = { b: 4, c:5 };
const returnTarget = Object.assign({}, source1, source2);

console.log(returnTarget);
// Object {a: 1, b: 4, c: 5}

๐Ÿ”ต Object.keys(), Object.values(), Object.entries()

  • Object.Keys(๊ฐ์ฒด)
    : ๊ฐ์ฑ„์˜ key๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋กœ ๋ฐ˜ํ™˜ํ•จ
  • Object.values(๊ฐ์ฒด)
    : ๊ฐ์ฑ„์˜ value๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋กœ ๋ฐ˜ํ™˜ํ•จ
  • Object.entries(๊ฐ์ฒด)
    : ๊ฐ์ฑ„์˜ key,value๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋กœ ๋ฐ˜ํ™˜ํ•จ

๐Ÿ‘พ #1

const user = {
  name: 'Zooyaho',
  age: 24,
  gender: 'female'
}

console.log(Object.keys(user)) // ['name', 'age', 'gender']

console.log(Object.values(user)) // ['Zooyaho', 24, 'female']

console.log(Object.entries(user)) // [['name','Zooyaho'],['age', 24],['gender','female']]

๐Ÿ”ต Object.fromEntries()

  • Object.fromEntries(๋ฐฐ์—ด)
    : ๊ฐ์ฑ„์˜ key,value ๋ฐฐ์—ด์„ ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ํ•จ

๐Ÿ‘พ #1

const arr =
[
  ['name','Zooyaho'],
  ['age', 24],
  ['gender','female']
]

console.log(Object.fromEntries(arr));
// { name: 'Zooyaho', age: 24, gender: 'female'}

๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋„ ์ธ๋ฑ์‹ฑ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•จ!!!!

console.log(user['email']) // thisis@gmail.com

const values = keys.map(key => user[key])
console.log(values)
// ['Zooyaho', 24, 'thisis@gmail.com']

5. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น(Destructuring assignment) == ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น

๐Ÿ”ต ๊ฐ์ฒด๋ฐ์ดํ„ฐ์—์„œ์˜ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

  • ๊ฐ์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๋‚ด์šฉ์„ ๊ตฌ์กฐ๋ถ„ํ•ดํ•ด์„œ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์†์„ฑ๋“ค๋งŒ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜์žˆ์Œ.
  • ๊ฐ์ฒด์˜ ์†์„ฑ์ด ๋งŽ์„ ๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ๊บผ๋‚ด์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.
  • const { key1, key2, key3, ...} = ๊ฐ์ฒด๋ช…
    -> ${๊ฐ์ฒด๋ช….key1} ์•„๋‹Œ ${key1}๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ = ๊ฐ„ํŽธํ•ด์ง.

๐Ÿ‘พ #1

const user = {
  name = 'Zooyaho',
  age: 85,
  email: 'thisis@gmail.com'
}

cost {name, age, email, address} = user

๐Ÿ“Ž ๋ณ„๋ช… ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉ
const { key1: nickname, key2, key3, ...} = ๊ฐ์ฒด๋ช…
-> ${key1}์ด ์•„๋‹Œ ์ง€์ •ํ•œ ๋ณ€์ˆ˜ ${nickname}์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, key1๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ(error)

๐Ÿ“Ž ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •
const { key1 = 'zooyaho', key2, key3, ...} = ๊ฐ์ฒด๋ช…
-> ${key1} ๋ฐ˜ํ™˜ ์‹œ undefined๊ฐ€ ์•„๋‹Œ zooyaho๋กœ ๋ฐ˜ํ™˜ ๋จ
-> ๊ฐ์ฒด์˜ key1์— ๊ฐ’์ด ์ง€์ •๋˜์–ด ์žˆ์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ ๋ฌด์‹œ๋จ.

๐Ÿ”ต ๋ฐฐ์—ด๋ฐ์ดํ„ฐ์—์„œ์˜ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

  • const [a, b, c, d] = ๊ฐ์ฒด
    -> ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๊ฐ๊ฐ a, b, c, d๋กœ ์ง€์ •ํ•˜์—ฌ
    console.log(a,b,c,d)๋กœ ๋ฐฐ์—ด ๊ฐ’๋“ค์„ ์ถœ๋ ฅํ•จ!
  • ์ด๋ฆ„์ด ์•„๋‹Œ ์ˆœ์„œ๋Œ€๋กœ ๊ตฌ์กฐ ๋ถ„ํ•ด๋ฅผ ํ•จ!!

๐Ÿ‘พ #1

const fruits = ['Apple', 'Banana', 'Cherry']
const [a, b, c, d] = fruits 
console.log(a,b,c,d) // Apple Banana Cherry undefined

๐Ÿ“Ž ํŠน์ • ์ธ๋ฑ์Šค๋ฒˆํ˜ธ๋งŒ ๊ตฌ์กฐ ๋ถ„ํ•ดํ•  ๊ฒฝ์šฐ

  • 1์ธ๋ฑ์Šค๋งŒ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ํ•˜๋ ค๋ฉด ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ  ์‰ผํ‘œ๋งŒ ์ž‘์„ฑํ•จ!

๐Ÿ‘พ #2

const [, b] = fruits
console.log(b) // Banana

6. ์ „๊ฐœ ์—ฐ์‚ฐ์ž(Spread) ...

  • ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋ฐ์ดํ„ฐ๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฐ๊ฐ์˜ ์•„์ดํ…œ์œผ๋กœ ์ „๊ฐœํ•ด์„œ ์ถœ๋ ฅํ•จ

๐Ÿ‘พ #1

const fruits = {'Apple', 'Banana', 'Cherry'}
console.log(...fruits) // Appple Banana Cherry

function toObjsct(a,b,c) {
  return {
    a:a,
    b:b,
    c:c
  }
}
console.log(toObjsect(...fruits)) 
// {a: 'Apple', b:'Banana', c:'Cherry'}

๐Ÿ“Ž ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ ์ „๊ฐœ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•˜๊ธฐ

  • rest parameter: ๋‚˜๋จธ์ง€์˜ ๋ชจ๋“  ์ธ์ˆ˜๋ฅผ ๋‹ค ๋ฐ›์•„๋‚ด๋Š” ์—ญํ• 
  • ๋ฐฐ์—ด์˜ ๋‚˜๋จธ์ง€ item์„ ๋„ฃ์€ ์ธ์ˆ˜๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•จ.

๐Ÿ‘พ #2

const fruits = {'Apple', 'Banana', 'Cherry', 'Orange'}

function toObjsct(a,b,...c) {
  return {
    a:a,
    b:b,
    c:c
  }
}
console.log(toObjsect(...fruits)) 
// {a: 'Apple', b:'Banana', c:Array(2)}

/* ์ถ•์•ฝํ˜• */
// 1. ์†์„ฑ์˜ ์ด๋ฆ„์™€ ๋ฐ์ดํ„ฐ์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์ถ•์•ฝ๊ฐ€๋Šฅ
function toObject(a, b, ...c) {
return { a, b, c }
}
// 2. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
const toObject = (a, b, ...c) => ({ a, b, c })
// {}๋Š” ํ•จ์ˆ˜์˜ ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์ˆœํ•œ ๋ธ”๋ก์˜ ์˜๋ฏธ๋กœ ํ•ด์„๋˜๊ธฐ ๋•Œ๋ฌธ์— ()์„ ๋ถ™์—ฌ์•ผ ๊ฐ์ฒด๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•จ!!!

7. ๋ฐ์ดํ„ฐ ๋ถˆ๋ณ€์„ฑ(Immutability)

  • ์›์‹œ ๋ฐ์ดํ„ฐ: String, Number, Boolean, undefined, null
  • ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ: Object, Array, Function
  • ์›์‹œ๋ฐ์ดํ„ฐ๋Š” ๋ถˆ๋ณ€์„ฑ, ์ฐธ์กฐํ˜•์€ ๊ฐ€๋ณ€์„ฑ์„ ๋ˆ๋‹ค.

๐Ÿ“Ž ์›์‹œ ๋ฐ์ดํ„ฐ

๐Ÿ‘พ #1

let a = 1
let b = 4
b = a
console.log(a === b) // true

๐Ÿ‘‰๐Ÿป b์˜ ๊ฐ’์ธ 1์„ ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด์„œ ํ• ๋‹นํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ธฐ์กด์— ์žˆ๋˜ 1์˜ ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์ด๋‹ค. b๋Š” ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๊ฒƒ์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ์˜ ์ฃผ์†Œ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ ๊ฒƒ์ด๋‹ค.

๐Ÿ“Ž ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ

๐Ÿ‘พ #2

let a = { k:1 }
let b = { k:1 }
console.log( a === b) // false - ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

b = a
console.log( a === b) // true - ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

let c = b
console.log( a === c) // true
a.k = 9
console.log( a === c) // true
console.log(a,b,c) // {k:9}{k:9}{k:9}

8. ์–•์€ ๋ณต์‚ฌ์™€ ๊นŠ์€ ๋ณต์‚ฌ

๐Ÿ”ต ์–•์€ ๋ณต์‚ฌ(shallow copy)

๐Ÿ‘พ #1

const user = {
  name = 'Zooyaho',
  age: 85,
  emails: ['thisis@gmail.com']
}
// ์–•์€ ๋ณต์‚ฌ
const copyUser = {...user}
// const copyUser = Object.assign({}, user) 
console.log(copyUser === user) // false

// emails์€ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ณต์œ ํ•˜๊ณ  ์žˆ์Œ
user.emails.push('zoo@naver.com')
console.log(copyUser.emails === user.emails) // true

๐Ÿ‘‰ ์–•์€ ๋ณต์‚ฌ๋Š” ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ๊นŒ์ง€ ๋ณต์‚ฌ๊ฐ€ ๋˜์ง€ ์•Š์Œ.

๐Ÿ”ต ๊นŠ์€ ๋ณต์‚ฌ(deep copy)

  • lodash ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊นŠ์€๋ณต์‚ฌ ์ง„ํ–‰ ๊ฐ€๋Šฅ
  • cloneDeep() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ(_.cloneDeep(๊ฐ์ฒด) : ๊ฐ์ฒด ๋ฐ˜ํ™˜)

๐Ÿ‘พ #2

const user = {
  name = 'Zooyaho',
  age: 85,
  emails: ['thisis@gmail.com']
}
// ๊นŠ์€ ๋ณต์‚ฌ
const copyUser = _.cloneDeep(user)
console.log(copyUser === user) // false

// emails์€ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ณต์œ ํ•˜๊ณ  ์žˆ์ง€ ์•Š์Œ
user.emails.push('zoo@naver.com')
console.log(copyUser.emails === user.emails) // false
profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

0๊ฐœ์˜ ๋Œ“๊ธ€