[3] Object

Naakiteยท2022๋…„ 2์›” 7์ผ
0

๐Ÿ“๋“œ๋ฆผ์ฝ”๋”ฉ JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
3/6
post-thumbnail

โœ… Object

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ์ฒด ๊ธฐ๋ฐ˜์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฑฐ์˜ '๋ชจ๋“  ๊ฒƒ' ์ด ๊ฐ์ฒด์ด๋‹ค. ๊ฐ์ฒด๋Š” 0๊ฐœ ์ด์ƒ์˜ ํ”„๋กœํผํ‹ฐ๋กœ ๊ตฌ์„ฑ๋œ ์ง‘ํ•ฉ์ด๋ฉฐ, ํ”„๋กœํผํ‹ฐ๋Š” key์™€ value ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

var person = { name: 'ellie'};
// name์€ key ์ด๋ฉฐ, ellie์€ value ์ด๋‹ค.

key: ๋นˆ ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด ๋˜๋Š” ์‹ฌ๋ฒŒ ๊ฐ’.
value: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ’.

โœ… ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ๋ฐฉ๋ฒ•

console.log(ellie.name); // ellie
console.log(ellie['name']);  // ellie

ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์—๋Š” ๋งˆ์นจํ‘œ ํ‘œ๊ธฐ๋ฒ•๊ณผ ๋Œ€๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ•์ด ์žˆ๋‹ค.

โœ… ํ”„๋กœํผํ‹ฐ ๊ฐ’ ๊ฐฑ์‹ , ๋™์ ์ƒ์„ฑ, ์‚ญ์ œ

//1. ๊ฐฑ์‹ 
person.name = 'Kim'; // {name: "Kim"}
//2. ๋™์  ์ƒ์„ฑ
person.age = 20; // person ๊ฐ์ฒด์—๋Š” age ํ”„๋กœํผํ‹ฐ๊ฐ€ ์กด์žฌํ•˜์ง€์•Š๋Š”๋‹ค.
                // ๋”ฐ๋ผ์„œ person ๊ฐ์ฒด์— age ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜๊ณ  ๊ฐ’์ด ํ• ๋‹น๋œ๋‹ค.
                // {name: "Kim", age: 20}
//3. ์‚ญ์ œ
delete person.age; // age ํ”„๋กœํผํ‹ฐ๊ฐ€ ์‚ญ์ œ๋œ๋‹ค.                 

โœ… Property value shorthand

const person1 = {name: 'bob', age:2};
const person2 = {name: 'steve', age:3};
const person3 = {name: 'dave', age:4}; // ๋งค๋ฒˆ ์„ ์–ธํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฑธ๊นŒ?

function Person(name, age){ // Constructor Function 
    // this = {};
    this.name = name;
    this.age= age;
    // return this;
} // ์ด๋ ‡๊ฒŒ ํ•˜๊ณ ๋‚˜์„œ

const person4 = new Person('ellie', 30); // ์ด๋ ‡๊ฒŒ ์„ ์–ธํ•˜๋ฉด๋œ๋‹ค.

โœ… in operator

// in operator : ํ•ด๋‹น ํ•˜๋Š” ํ‚ค๊ฐ€  ์˜ค๋ธŒ์ ํŠธ ์•ˆ์— ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธ.
console.log('name' in ellie); // true or false ๋ฐ˜ํ™˜

โœ… for..in vs for..of

for (key in ellie) {
    console.log(key);    
}


const array =[1,2,3,4,5];
for (let i=0; i<array.length; i++) {
     console.log(array[i]);
} // for..of ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์‰ฝ๊ฐœ ํ•ด๊ฒฐ๋œ๋‹ค!

for (value of array){
    console.log(value);
}  

๋“œ๋ฆผ์ฝ”๋”ฉ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.๐Ÿ‘

profile
๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿƒโ€โ™€๏ธ

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