๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

javascript ๊ฐ์ฒด(Object) (2023.04.28) ๋ณธ๋ฌธ

full stack/JavaScript

javascript ๊ฐ์ฒด(Object) (2023.04.28)

Shinjio 2023. 4. 29. 20:03

๐ŸŽˆ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด(object)

 

์—ฌ๋Ÿฌ ์†์„ฑ(property)์„ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…

๋ฐ์ดํ„ฐ(์†์„ฑ)๊ณผ ๊ทธ ๋ฐ์ดํ„ฐ์— ๊ด€๋ จ๋˜๋Š” ๋™์ž‘(์ ˆ์ฐจ, ๋ฐฉ๋ฒ•, ๊ธฐ๋Šฅ)์„ ๋ชจ๋‘ ํฌํ•จํ•œ ๊ฐœ๋…

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ํƒ€์ž…(data type)์€ ๊ฐ์ฒด(object)

๊ฐ์ฒด๋ž€ ์ด๋ฆ„(name ํ˜น์€ key)๊ณผ ๊ฐ’(value)์œผ๋กœ ๊ตฌ์„ฑ๋œ ํ”„๋กœํผํ‹ฐ(property)์˜ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ง‘ํ•ฉ

ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์œผ๋กœ ํ•จ์ˆ˜๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋„ ์žˆ์Œ (์ด๋ฅผ ๋ฉ”์†Œ๋“œ๋ผ๊ณ  ํ•จ)

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์ˆซ์ž, ๋ฌธ์ž์—ด, ๋ถˆ๋ฆฌ์–ธ, undefined ํƒ€์ž…์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฒƒ์ด ๊ฐ์ฒด  

 

๐Ÿ“– ๊ฐ์ฒด์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

 

๐Ÿ“– ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์ ‘๊ทผ ๋ฐ ์ˆ˜์ •

 

// ๊ฐ์ฒด : ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…

// ๊ตฌ์กฐ -> {key1 : value1, key2 : value2, key3 : value3 ... }
// key -> ๊ฐ์ฒด์˜ ์†์„ฑ(property)
// value -> ํ•ด๋‹นํ•˜๋Š” ์†์„ฑ์˜ ๊ฐ’ / ๊ธฐ๋ณธ์ž๋ฃŒํ˜•, ํ•จ์ˆ˜, ๋ฐฐ์—ด, ๊ฐ์ฒด๊นŒ์ง€ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋‹ด์„ ์ˆ˜ ์žˆ์Œ

// ๊ฐ์ฒด ์ƒ์„ฑ
let person = {
    // key ๊ฐ’์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ""๋ฅผ ๋ถ™์—ฌ๋„ ๋˜๊ณ  ์•ˆ๋ถ™์—ฌ๋„ ๋œ๋‹ค.
    // ๋ฐ˜๋“œ์‹œ value ๋’ค์—๋Š” ์ฝค๋งˆ(,)!! 
    name : "ํ™๊ธธ๋™",
    age : 30,
    isPerson : true,
    friday : ()=>{
        console.log('์—ฌ๋Ÿฌ๋ถ„ ๊ธˆ์š”์ผ์ด์—์š”!๐Ÿ˜˜');
    },
    favorite : {
        food : "pizza๐Ÿ•",
        drink : "coffeeโ˜•",
        color: "violet๐Ÿ’œ"
    },
    teacher : ['์ž๋ฐ”์Œค๐Ÿ‘ฉ', 'ํŒŒ์ด์ฌ์Œค๐Ÿ‘ง', '์›น์Œค๐Ÿ‘ฉ‍๐Ÿฆฐ']
};

// ๊ฐ์ฒด ์ ‘๊ทผ
// Object.key (๊ฐ์ฒด์ด๋ฆ„.ํ‚ค์ด๋ฆ„)
console.log(person);
console.log('์ด๋ฆ„: ', person.name);
console.log('๋‚˜์ด: ', person.age);
console.log('์‚ฌ๋žŒ: ', person.isPerson);
person.friday();
console.log('์ข‹์•„ํ•˜๋Š” ์Œ๋ฃŒ : ', person.favorite.drink);
console.log('๋‹ด์ž„์„ ์ƒ๋‹˜: ', person.teacher[0]);

// ์†์„ฑ๊ฐ’ ๋ณ€๊ฒฝ ๋ฐ ์ถ”๊ฐ€
person.name = "์ง€์˜์ด";
console.log('์ด๋ฆ„ ๋ณ€๊ฒฝ: ', person.name);
person.height = 165;
console.log('ํ‚ค: ', person.height);
// weight ์†์„ฑ๊ฐ’ ์ถ”๊ฐ€!
person.weight = 100;
console.log('๋ชธ๋ฌด๊ฒŒ: ', person.weight);

 

๐Ÿ“– ๊ฐ์ฒด์˜ ์ข…๋ฅ˜

 

โš™๏ธ ๋‚ด์žฅ๊ฐ์ฒด(Built - in Object)

Number, Math, Date, String, Array ๋“ฑ์ด ์žˆ์Œ

 

// ๋‚ด์žฅ๊ฐ์ฒด : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด
// Array, Math, Date, ...

let today = new Date();
let yesterday = new Date(2023, 3, 27, 0, 0, 0);
// Date ๊ฐ์ฒด ์›” 0๋ถ€ํ„ฐ ์‹œ์ž‘
console.log('์˜ค๋Š˜ : ', today);
console.log('์–ด์ œ : ', yesterday);

// ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜์”ฉ ๊ฐ€์ ธ์˜ค๊ธฐ!
// ์—ฐ๋„ ์ถ”์ถœ (OO๋…„)
let tYear = today.getFullYear(); 
// ์›” ์ถ”์ถœ (O์›”(0๋ถ€ํ„ฐ ์‹œ์ž‘))
let tMonth = today.getMonth() + 1; 
// ์ผ ์ถ”์ถœ (O์ผ)
let tDate = today.getDate();
// ์š”์ผ ์ถ”์ถœ (O์š”์ผ) : 0~6๊ฐ’์ด ๋“ค์–ด๊ฐ€์žˆ์Œ + ์ผ์š”์ผ๋ถ€ํ„ฐ ์‹œ์ž‘ => 0 : ์ผ์š”์ผ
let tDay = today.getDay();
// ์‹œ๊ฐ„ ์ถ”์ถœ (O์‹œ)
let tHour = today.getHours();
// ๋ถ„ ์ถ”์ถœ (O๋ถ„)
let tMinute = today.getMinutes();
// ์ดˆ ์ถ”์ถœ (O์ดˆ)
let tSecond = today.getSeconds();

// ์š”์ผ 0~6 -> ์ผ~ํ† 

switch(tDay){
    case 0 : tDay = '์ผ'; break;
    case 1 : tDay = '์›”'; break;
    case 2 : tDay = 'ํ™”'; break;
    case 3 : tDay = '์ˆ˜'; break;
    case 4 : tDay = '๋ชฉ'; break;
    case 5 : tDay = '๊ธˆ'; break;
    case 6 : tDay = 'ํ† '; break;
}

// ์ถœ๋ ฅํ˜•ํƒœ -> ์ฝ˜์†”์ฐฝ
// OO๋…„ O์›” O์ผ O์š”์ผ
// O์‹œ O๋ถ„ O์ดˆ

console.log(`${tYear}๋…„ ${tMonth}์›” ${tDate}์ผ ${tDay}์š”์ผ`);
console.log(`${tHour}์‹œ ${tMinute}๋ถ„ ${tSecond}์ดˆ`);

 

โš™๏ธ ๋ธŒ๋ผ์šฐ์ € ๊ฐ์ฒด (Browser Object Model, BOM)

์›น ๋ธŒ๋ผ์šฐ์ €์™€ ๊ด€๋ จ๋œ ๊ฐ์ฒด์˜ ์ง‘ํ•ฉ

์ข…๋ฅ˜๋กœ๋Š” window(์ตœ์ƒ์œ„), location, navigator, history, screenm, document ๊ฐ€ ์žˆ์Œ

 

window

๋ชจ๋“  ๊ฐ์ฒด์˜ ์ตœ์ƒ์œ„ ๊ฐ์ฒด

์ƒˆ ์ฐฝ์„ ์ƒ์„ฑํ•˜๋Š” ์—ญํ•  ๋“ฑ์„ ํ•จ

 

location

๋ธŒ๋ผ์šฐ์ €์˜ ํ˜„์žฌ URL ์ฃผ์†Œ๊ฐ’์— ๋Œ€ํ•œ ์ •๋ณด ์ œ๊ณต

 

screen 

์Šคํฌ๋ฆฐ(๋ชจ๋‹ˆํ„ฐ)์— ๋Œ€ํ•œ ์†์„ฑ ์ œ 

 

navigator

๋ธŒ๋ผ์šฐ์ €์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณต

 

history

๋ธŒ๋ผ์šฐ์ €๋กœ ๋ฐฉ๋ฌธํ•œ ํŽ˜์ด์ง€๋“ค์˜ ์ •๋ณด ์ œ๊ณต

 

// BOM (Browser Object Model)
// ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด

// window
//  : ๋ธŒ๋ผ์šฐ์ € ๊ฐ์ฒด ์ค‘ ์ตœ์ƒ์œ„ ๊ฐ์ฒด, ๋ธŒ๋ผ์šฐ์ € ์ฐฝ ๊ด€๋ จ ๊ธฐ๋Šฅ
// ์ฃผ์†Œ, ์ฃผ์†Œ์˜ ์ œ๋ชฉ, ํฌ๊ธฐ ์„ค์ • ๊ฐ€๋Šฅ
// window. ->  ์ƒ๋žต ๊ฐ€๋Šฅ
// window.open("http://naver.com", "๋„ค์ด๋ฒ„", "width=500; height=500");
// open("http://naver.com", "๋„ค์ด๋ฒ„", "width=500; height=500"); => ์ •์ƒ์ž‘๋™!

// location
// : ๋ธŒ๋ผ์šฐ์ €์˜ ์ฃผ์†Œ ๊ด€๋ จ ์ •๋ณด
console.log(location);
console.log('ํ˜„์žฌ ์ฃผ์†Œ :', location.href);
// ํŽ˜์ด์ง€ ์ด๋™
// location.href = 'http://www.smhrd.or.kr'

// screen 
// : ํ™”๋ฉด ํ•ด์ƒ๋„์™€ ๊ด€๋ จ๋œ ์ •๋ณด (๋„ˆ๋น„, ๋†’์ด)
console.log(screen);

// navigator
// : ํ˜„์žฌ ๋ธŒ๋ผ์šฐ์ € ์ •๋ณด(๋ฒ„์ „, ์ด๋ฆ„, ์œ„์น˜ ๋“ฑ)
console.log(navigator);

// history 
// : ๋ธŒ๋ผ์šฐ์ €์˜ ์ด๋™๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ (๋’ค๋กœ, ์•ž์œผ๋กœ)
// back() - ๋’ค๋กœ
// forward() - ์•ž์œผ๋กœ 
// go(์ด๋™ํ•  ํŽ˜์ด์ง€์˜ ์ˆซ์ž) - ํŽ˜์ด์ง€๋กœ ์ด๋™ 
//    ex) 2 : ์•ž์œผ๋กœ ๋‘๋ฒˆ, -3 : ๋’ค๋กœ ์„ธ๋ฒˆ