๐Ÿ“’Javascript ๊ฐ์ฒด :)

zooyahoยท2022๋…„ 4์›” 6์ผ
0

study with me

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

Object

  • ๊ฐ์ฒด(์ฐธ์กฐ) ํƒ€์ž…์˜ ๊ฐ’, ์ฆ‰ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๊ฐ’์ด๋‹ค.
  • ์ฐธ์กฐ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ฐธ์กฐ๊ฐ’์€ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์˜ ์ฃผ์†Œ๋‹ค!!
  • ์›์‹œ๊ฐ’๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‹๋ณ„์ž๊ฐ€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ถ€์ž‘์šฉ์ด ์žˆ๋‹ค.
  • ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ Key:Value ํ˜•ํƒœ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. { }
  • method: ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ๋กœ ํ• ๋‹น ๋œ ํ•จ์ˆ˜
const superman = {
  // this = {}
  name: 'clark',
  age: 32,
  fly: function(){
  	console.log("๋‚ ์•„๊ฐ‘๋‹ˆ๋‹ค.");
  }
  // return this
}
  • ๋งˆ์ง€๋ง‰์— ์‰ผํ‘œ(,)ํ•ด์•ผ ์ด๋™, ์‚ญ์ œ ์‹œ ์œ ์šฉํ•จ.

๐Ÿ”ต ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ(Computed property)

๐Ÿ‘พ #1

let a = 'age';

const user = {
  name : 'Zooyaho',
  [a] : 30 // age: 30
}

๐Ÿ‘พ #2

const user = {
  [1+4] : 5,
  ["์•ˆ๋…•" + "ํ•˜์„ธ์š”"]: "Hello"
}

console.log(user); // {5:5, ์•ˆ๋…•ํ•˜์„ธ์š”: "Hello"}

๐Ÿ”ต ์ ‘๊ทผ, ์ถ”๊ฐ€, ์‚ญ์ œ

๐Ÿ‘พ #์ ‘๊ทผ

superman.name; // 'clark'
superman['age']; // 32

๐Ÿ‘พ #์ถ”๊ฐ€

superman.gender = 'male';
superman['hairColor'] = 'black';

๐Ÿ‘พ #์‚ญ์ œ

delete superman.hairColor;

๐Ÿ”ต ๋‹จ์ถ• ํ”„๋กœํผํ‹ฐ

const name = 'clark';
const age = 32;

/* const superman = {
  name : name,
  age : age,
  gerder: 'male',
} */

const superman = {
  name,
  age,
  gender: 'male',
}

๐Ÿ”ต ํ”„๋กœํผํ‹ฐ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ

const superman = {
  name: clark,
  age: 32,
}

console.log(superman.birthDay); // undefined

๐Ÿ“Œ in ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ

  • ์–ด๋–ค๊ฐ’์ด ๋„˜์–ด ์˜ค๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์„๋•Œ in์—ฐ์‚ฐ์ž ์‚ฌ์šฉ

๐Ÿ‘พ#1

console.log('birthDay' in superman); // false

๐Ÿ‘พ#2

function makeObject(name, age) {
  return {
    name,
    age,
    hobby: 'football'
  };
}

const Mike = makeObject("Mike", 30);
console.log(Mike);
// Object { age:30, hobby: "football", name: "Mike"}

console.log("age" in Mike); // true
console.log("birthDay" in Mike); // false

๐Ÿ”ต for ... in ๋ฐ˜๋ณต๋ฌธ

const superman = {
  name: 'clark',
  age: 32,
}

for( key in superman){
  console.log(key); // "name"
  console.log(superman[key]) // "clark"
}

๐Ÿ”ต ๋ฉ”์„œ๋“œ

/* 
fly: function(){
  	console.log("๋‚ ์•„๊ฐ‘๋‹ˆ๋‹ค.");
  }
*/

const superman = {
  name: 'clark',
  age: 32,
  fly(){
  	console.log("๋‚ ์•„๊ฐ‘๋‹ˆ๋‹ค.");
  }
}

๐Ÿ“Œ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ this

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜์™€๋Š” ๋‹ฌ๋ฆฌ ์ž์‹ ๋งŒ์˜ this๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์Œ.
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ this๋Š” ์™ธ๋ถ€์—์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ด.
    ๐Ÿ‘‰๐Ÿป ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ!!!

๐Ÿ‘พ#1

const boy = {
  name: 'Mike',
  sayHello: function() {
  	console.log(`Hello, I'm ${this.name}`)
  }
}
boy.sayHello(); // Hello, I'm Mike

๐Ÿ‘‰๐Ÿป ์ผ๋ฐ˜ํ•จ์ˆ˜ ๋‚ด์—์„œ์˜ this๋Š” ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด

๐Ÿ‘พ#2

const girl = {
  name: 'Jane',
  sayHello: () => {
  	console.log(`Hello, I'm ${this.name}`)
  }
}
girl.sayHello(); // Hello, I'm undefined

๐Ÿ‘‰๐Ÿป ํ™”์‚ดํ‘œํ•จ์ˆ˜ ๋‚ด์—์„œ์˜ this๋Š” ์ „์—ญ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด

profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

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