์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋…ธ๋“œ 2ํŒ€] #2.JavaScript ๋ฐ˜๋ณต๋ฌธ, ๋ฐ˜๋ณต์ž, ๊ฐ์ฒด

24-25/Node.js 2

by sksmsyena 2024. 10. 4. 10:00

๋ณธ๋ฌธ

728x90

๐ŸŒŸํ‚ค์›Œ๋“œ: ๋ฐ˜๋ณต๋ฌธ, ๋ฐ˜๋ณต์ž(iterators), ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ, ๊ฐ์ฒด, getter & setter


 

1. ๋ฐ˜๋ณต๋ฌธ

๋ฐ˜๋ณต๋ฌธ์€ for๋ฐ˜๋ณต๋ฌธ, while ๋ฐ˜๋ณต๋ฌธ, do~while ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ด 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.
 
 
1-1. for ๋ฐ˜๋ณต๋ฌธ

- for ๋ฐ˜๋ณต๋ฌธ์€ ์„ธ๋ฏธ์ฝœ๋ก (;)์œผ๋กœ ๊ตฌ๋ถ„๋œ 3๊ฐœ์˜ ํ‘œํ˜„์‹์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

1) ์ดˆ๊ธฐํ™”: ๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐํ™”๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.
2) ์ค‘์ง€ ์กฐ๊ฑด: ๋ฐ˜๋ณต๋ฌธ์„ ๊ณ„์† ์‹คํ–‰ํ• ์ง€ ์ค‘์ง€ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์กฐ๊ฑด์ด๋‹ค.
3) ์ฆ๊ฐ์‹: ๋ฐ˜๋ณต๋ฌธ์ด ํ•œ ๋ฒˆ ์‹คํ–‰๋œ ํ›„, ๋ฐ˜๋ณต๋ฌธ์˜ ๋‹ค์Œ ์‚ฌ์ดํด๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ์ฃผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ฑฐ๋‚˜ ๊ฐ์†Œ์‹œํ‚ค๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

for (let i=5; i<=10; i++) {
  console.log(i);
}

 
 

1-2. while ๋ฐ˜๋ณต๋ฌธ
- while ๋ฐ˜๋ณต๋ฌธ์€ ํŠน์ • ์กฐ๊ฑด์ด true์ผ ๋™์•ˆ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฃจํ”„ ๊ตฌ์กฐ์ด๋‹ค.
- for ๋ฐ˜๋ณต๋ฌธ๊ณผ ๋‹ฌ๋ฆฌ, ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์‚ฌ์ „์— ์•Œ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์— ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.
- ๋™์ž‘ ๋ฐฉ์‹: while์˜ ์กฐ๊ฑด ํ‰๊ฐ€ → ์กฐ๊ฑด์ด true์ธ ๊ฒฝ์šฐ ๋ธ”๋ก({ }) ์•ˆ์˜ ์ฝ”๋“œ ์‹คํ–‰ → ์กฐ๊ฑด์ด false๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ณ , false๊ฐ€ ๋˜๋ฉด ์ข…๋ฃŒ

let counter = 0;

while (counter < 5) { // counter๊ฐ€ 5๋ณด๋‹ค ์ž‘์€ ๋™์•ˆ ๋ฐ˜๋ณต
  console.log("Counter: " + counter);
  counter++; // counter๋ฅผ 1์”ฉ ์ฆ๊ฐ€
}

 
 
1-3. do~while ๋ฐ˜๋ณต๋ฌธ
- do while ๋ฐ˜๋ณต๋ฌธ์€ while ๋ฐ˜๋ณต๋ฌธ์˜ ๋ณ€ํ˜•์œผ๋กœ, ๋ธ”๋ก ์•ˆ์˜ ์ฝ”๋“œ๊ฐ€ ์ตœ์†Œ ํ•œ ๋ฒˆ์€ ์‹คํ–‰๋˜๋„๋ก ๋ณด์žฅํ•œ๋‹ค.

let counter = 0;

do {
  console.log("Counter: " + counter);
  counter++; // counter๋ฅผ 1์”ฉ ์ฆ๊ฐ€
} while (counter < 5); // counter๊ฐ€ 5๋ณด๋‹ค ์ž‘์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

 


2. ๋ฐ˜๋ณต์ž(Iterators)

๋ฐ˜๋ณต์ž๋ž€, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฐฐ์—ด์ด๋‚˜ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ฐ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.
 
 
2-1. ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ธฐ
  ์•„๋ž˜์—์„œ ๋ฐฐ์šธ forEach, map, filter ๊ฐ™์€ ๋ฐ˜๋ณต์ž ๋ฉ”์†Œ๋“œ๋“ค์€ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ด๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋“ค์€ ๋ฐฐ์—ด์ด๋‚˜ ๋‹ค๋ฅธ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ๋ฐ˜๋ณต์ ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด๋•Œ, ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐœ๋…์ด ํ•„์š”ํ•˜๋‹ค.
 
- ์•„๋ž˜์˜ ์˜ˆ์ œ ์ฝ”๋“œ์ฒ˜๋Ÿผ ํ•จ์ˆ˜ ์ด๋ฆ„์ด ๋„ˆ๋ฌด ๊ธด ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ, ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์งง์€ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

โ€ป ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๋•Œ๋Š”, ํ•จ์ˆ˜์˜ ์ด๋ฆ„ ๋’ค์— ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค.

const IwanttosayhellotoChris = () => { //ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด ๋„ˆ๋ฌด ๊ธธ๋‹ค.
  console.log('Hello!');
};

// ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ธฐ
const sayHello = IwanttosayhellotoChris;

// ํ• ๋‹น๋œ ํ•จ์ˆ˜ ํ˜ธ์ถœ
sayHello(); // 'Hello!' ์ถœ๋ ฅ
console.log(sayHello.name); // 'greet' ์ถœ๋ ฅ

 
 
2-2. ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด๋Š” ๊ฒฝ์šฐ vs ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

→ ๊ด„ํ˜ธ์˜ ์œ ๋ฌด๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋‚˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ ๋งค์šฐ ์ค‘์š”ํ•˜๋ฏ€๋กœ ๊ผญ ์ž˜ ๊ตฌ๋ถ„ํ•˜์—ฌ ์•Œ์•„๋‘์–ด์•ผ ํ•œ๋‹ค.
โ‘  ํ•จ์ˆ˜ ์ด๋ฆ„ ๋’ค์— ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š” ๊ฒฝ์šฐ (ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋Œ€์ž…)

const greet = () => {
  console.log('Hello!');
};

const sayHello = greet; // ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋ณ€์ˆ˜์— ๋Œ€์ž… (์‹คํ–‰ํ•˜์ง€ ์•Š์Œ)
sayHello(); // 'Hello!' ์ถœ๋ ฅ (์—ฌ๊ธฐ์„œ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋จ)

 
- ๊ด„ํ˜ธ ์—†์ด ํ•จ์ˆ˜๋ช…์„ ๋Œ€์ž…ํ•˜๋Š” ๊ฒฝ์šฐ, ํ•จ์ˆ˜ ๊ทธ ์ž์ฒด๊ฐ€ ๋ณ€์ˆ˜์— ์ €์žฅ๋œ๋‹ค.
- ์ด๋•Œ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š๊ณ , ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚˜์ค‘์— ํ•จ์ˆ˜๋กœ์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
 
 
โ‘ก ํ•จ์ˆ˜ ์ด๋ฆ„ ๋’ค์— ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด๋Š” ๊ฒฝ์šฐ (ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„ ๋ฐ˜ํ™˜๊ฐ’์„ ๋Œ€์ž…)

const greet = () => {
  console.log('Hello!');
};

const sayHello = greet(); // ํ•จ์ˆ˜๊ฐ€ ์ฆ‰์‹œ ํ˜ธ์ถœ๋˜๊ณ , ๋ฐ˜ํ™˜๊ฐ’์ด ๋ณ€์ˆ˜์— ๋Œ€์ž…๋จ
console.log(sayHello); // undefined ์ถœ๋ ฅ (greet ํ•จ์ˆ˜๋Š” ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— undefined)

 
- ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด ํ•จ์ˆ˜๊ฐ€ ์ฆ‰์‹œ ํ˜ธ์ถœ๋œ๋‹ค.
- ๊ทธ ๊ฒฐ๊ณผ๋กœ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด ๋ณ€์ˆ˜์— ๋Œ€์ž…๋ฉ๋‹ˆ๋‹ค. greet ํ•จ์ˆ˜๋Š” ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— undefined๊ฐ€ sayHello์— ๋Œ€์ž…๋œ๋‹ค.
- ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ, ๊ทธ ๊ฐ’์ด ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ๋‹ค.
 
 

∴ ์ฐจ์ด๋ฅผ ์š”์•ฝํ•˜๋ฉด,

  • ๊ด„ํ˜ธ ์—†์ด ํ•จ์ˆ˜๋ช…์„ ๋Œ€์ž…: ํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ ๋Œ€์ž…๋˜์–ด, ๋‚˜์ค‘์— ๊ทธ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ด„ํ˜ธ๋ฅผ ๋ถ™์—ฌ ํ•จ์ˆ˜ ํ˜ธ์ถœ: ํ•จ์ˆ˜๊ฐ€ ์ฆ‰์‹œ ์‹คํ–‰๋˜๊ณ , ๊ทธ ๋ฐ˜ํ™˜๊ฐ’์ด ๋ณ€์ˆ˜์— ๋Œ€์ž…๋œ๋‹ค.

 
2-3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ 5๊ฐ€์ง€

โ‘  .forEach() ๋ฉ”์†Œ๋“œ
- ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ํ˜ธ์ถœํ•œ๋‹ค.

const fruits = ["London", "Paris", "Japan"];

fruits.forEach((fruit) =>
  console.log("I want to go " + place);
);
/* ์ถœ๋ ฅ ๊ฒฐ๊ณผ
I want to go London.
I want to go Paris.
I want to go Japan.
*/

 
 

โ‘ก .map() ๋ฉ”์†Œ๋“œ
- ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ํ˜ธ์ถœํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค. ์›๋ณธ ๋ฐฐ์—ด์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.

const numbers = [1, 2, 3, 4, 5];

const doubled = numbers.map(number => number * 2);

console.log(doubled); 
//์ถœ๋ ฅ ๊ฒฐ๊ณผ: [2, 4, 6, 8, 10]

 
 

โ‘ข .filter() ๋ฉ”์†Œ๋“œ
- ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค. ์›๋ณธ ๋ฐฐ์—ด์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.

const words = ['apple', 'banana', 'kiwi', 'cherry', 'grape', 'orange'];

const longWords = words.filter(word => word.length > 5);

console.log(longWords);
//์ถœ๋ ฅ ๊ฒฐ๊ณผ: ['banana', 'cherry', 'orange']

 
 

โ‘ฃ .findIndex() ๋ฉ”์†Œ๋“œ
- ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งŒ์•ฝ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ์—†์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const numbers = [10, 20, 30, 40, 50];

const index = numbers.findIndex(number => number > 25); // 25๋ณด๋‹ค ํฐ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์Œ

console.log(index);
//์ถœ๋ ฅ ๊ฒฐ๊ณผ: 2

 
 

โ‘ค .reduce() ๋ฉ”์†Œ๋“œ
- ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋‹จ์ผ ๊ฐ’์œผ๋กœ ์ถ•์†Œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” ๋ˆ„์  ๊ฐ’๊ณผ ํ˜„์žฌ ์š”์†Œ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
* ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const numbers = [1, 2, 3, 4, 5];

const total = numbers.reduce((accumulator, currentValue) => {
  return accumulator + currentValue; //
}, 0); // ์ดˆ๊ธฐ ๊ฐ’์„ 0์œผ๋กœ ์„ค์ •

console.log(total); 
// ์ถœ๋ ฅ: 15

 
 


 

3. ๊ฐ์ฒด

3-1. ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด

- 'ํ‚ค: ๊ฐ’' ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘๊ด„ํ˜ธ{ } ๋กœ ๋ฌถ์–ด ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
โ€ป 'ํ‚ค: ๊ฐ’'๋“ค ์‚ฌ์ด๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง๊ฒƒ!

let Pubao = {
    species : "panda",
    color: "black and white"
};

 
 
3-2. ํ‚ค์˜ ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•

โ‘  '.' ์—ฐ์‚ฐ์ž
- ๊ฐ์ฒด์˜ ์ด๋ฆ„ ๋’ค์— ์ ์„ ๋ถ™์ด๊ณ , ์†์„ฑ ์ด๋ฆ„(ํ‚ค)์„ ์ž‘์„ฑํ•œ๋‹ค.

pubao.color

 
 

โ‘ก ๋Œ€๊ด„ํ˜ธ[ ] ํ‘œ๊ธฐ๋ฒ•
- ๊ฐ์ฒด์˜ ์ด๋ฆ„ ๋’ค์— ๋Œ€๊ด„ํ˜ธ๋ฅผ ์จ์ฃผ๊ณ , ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์†์„ฑ ์ด๋ฆ„(ํ‚ค)์„ ์ž‘์„ฑํ•œ๋‹ค.

pubao["color"]

 
 
* ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋”ฐ์˜ดํ‘œ ์—†์ด ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์จ์ฃผ๊ณ , ํ‚ค๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์จ์ค€๋‹ค.

const person = { name: "John", age: 30 };
const n = "name"

console.log(person[n]); // John
console.log(person["age"]);  // 30

 
 
3-3. ์†์„ฑ ํ• ๋‹น

if, ์†์„ฑ์ด ์ด๋ฏธ ๊ฐ์ฒด์— ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ → ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ๋จ
if, ์†์„ฑ์ด ๊ฐ์ฒด์— ์—†๋Š” ๊ฒฝ์šฐ → ์ƒˆ ์†์„ฑ์ด ๊ฐ์ฒด์— ์ถ”๊ฐ€๋จ

const person = { name: "John", age: 30 };

person.age = 45; //age์˜ ๊ฐ’์ด 45๋กœ ๋ณ€๊ฒฝ๋จ
person.job = "teacher"; //์ƒˆ๋กœ์šด ์†์„ฑ์ด ์ถ”๊ฐ€๋จ

 
 
3-4. ์†์„ฑ ์‚ญ์ œ
- delete ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†์„ฑ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

delete person.job;

 
 
3-5. ์ค‘์ฒฉ๋œ ๊ฐ์ฒด
โ€ป ๊ฐ์ฒด ์ค‘์ฒฉ์‹œ์—๋Š” ๊ฐ์ฒด ์‚ฌ์ด๋ฅผ ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ์žŠ์ง€๋ง์ž.

const library = {
  name: "City Library",
  address: {
    street: "123 Library St.",
    city: "Booktown",
    zipCode: 12345
  }
}

 
 
3-6. ๋ฉ”์†Œ๋“œ
- ๊ฐ์ฒด์— ์ €์žฅ๋œ ํ•จ์ˆ˜๋ฅผ '๋ฉ”์†Œ๋“œ'๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

let robotAssistant = { //robotAssistant๋ผ๋Š” ๊ฐ์ฒด ์•ˆ์— assist ๋ฉ”์†Œ๋“œ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.
  assist() {
    console.log("Activating assistance mode. How can I help you today?");
  }
};

 
 
3-7. this ํ‚ค์›Œ๋“œ

- ๋ฉ”์†Œ๋“œ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ ๊ฐ์ฒด์˜ ๋‹ค๋ฅธ ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

const student = {
  name: 'Emily',
  introduce() {
    return `Hi, I am ${this.name}`; //this ํ‚ค์›Œ๋“œ ํ•„์ˆ˜
  }
};

 
 
3-8. getter & setter

- getter: ๋‚ด๋ถ€ ์†์„ฑ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ
- setter: ์†์„ฑ ๊ฐ’์„ ์•ˆ์ „ํ•˜๊ฒŒ ์žฌํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ

์ •์ˆ˜๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ณ€์ˆ˜์— ์‹ค์ˆ˜๋กœ ๋ฌธ์ž์—ด์„ ์žฌํ• ๋‹นํ•˜๋ ค๊ณ  ํ•˜๋ฉด, ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด์™€ ๊ฐ™์€ ์‹ค์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด ์•ˆ์—์„œ setter ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํƒ€์ž… ์ฒดํฌ๋ฅผ ํ•˜์—ฌ, ๊ฐ’์ด ์˜ฌ๋ฐ”๋ฅธ ํƒ€์ž…์ธ์ง€ ํ™•์ธํ•œ ํ›„ ์†์„ฑ์„ ์žฌํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ getter ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์†์„ฑ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ๋„ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ๊ฐ’์„ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๋งŒ๋“ค์–ด ์•ˆ์ „ํ•˜๊ฒŒ ๊ฐ’์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

∴ ๋”ฐ๋ผ์„œ, getter & setter๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์†์„ฑ ๊ฐ’์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ฝ๊ณ  ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ์‹์„ ๋ชจ๋‘ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

const person = {
  age: 25, // ์†์„ฑ

  // Getter: ๋‚˜์ด ๋ฐ˜ํ™˜
  getAge() {
    return this.age;
  },

  // Setter: ๋‚˜์ด๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์„ค์ •
  setAge(newAge) {
    if (typeof newAge === 'number' && newAge >= 0) {
      this.age = newAge;
    } else {
      console.error('Error: Age must be a positive number.');
    }
  }
};

// Getter ์‚ฌ์šฉ
console.log(person.getAge()); // 25

// Setter ์‚ฌ์šฉ - ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’
person.setAge(30);
console.log(person.getAge()); // 30

// Setter ์‚ฌ์šฉ - ์ž˜๋ชป๋œ ๊ฐ’
person.setAge(-5); // Error: Age must be a positive number.

 


 

quiz
1. (   ), (   ) ๋ฉ”์†Œ๋“œ๋Š” ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 
2. ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” (   ) ์ด๋‹ค.
 
3. ๊ฐ์ฒด์— ์ €์žฅ๋œ ํ•จ์ˆ˜๋ฅผ (   )๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
 
4. (   ) ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.
 
5. ๋ฉ”์†Œ๋“œ ๋‚ด๋ถ€์—์„œ (   ) ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ ๊ฐ์ฒด์˜ ๋‹ค๋ฅธ ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
 
6. ํ‚ค์˜ ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด 2๊ฐ€์ง€๋กœ, (   ) ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ (   ) ํ‘œ๊ธฐ๋ฒ•์„ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.
 
7. ๋‚ด๋ถ€ ์†์„ฑ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” (   )์ด๊ณ , ์†์„ฑ ๊ฐ’์„ ์•ˆ์ „ํ•˜๊ฒŒ ์žฌํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๋Š” ( )์ด๋‹ค.
 

programming quiz
8. ๋‹ค์Œ ๋ฐฐ์—ด์— ๋‹ด๊ธด ์ˆซ์ž๋“ค์„ ํ•œ ์ค„์”ฉ ์ถœ๋ ฅํ•˜๋„๋ก forEach() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

const numbers = [10, 20, 30, 40, 50];

 
9. 'This is a Tesla S, and the battery level is at 85%.'์˜ ์ถœ๋ ฅ๋ฌธ์ด ๋‚˜์˜ค๋„๋ก ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์‹œ์˜ค.

const car = {
  brand: "Tesla",
  model: "S",
  batteryLevel: 85,
  provideInfo() {
    return //์ฑ„์šฐ๊ธฐ
  }
};

//์ฑ„์šฐ๊ธฐ

 
 


 
answer
1. map(), filter()
2. . ForEach()
3. ๋ฉ”์†Œ๋“œ
4. delete
5. this
6. '.', ๋Œ€๊ด„ํ˜ธ
7. getter, setter
8. 

const numbers = [10, 20, 30, 40, 50];

numbers.forEach((number) => {
  console.log(number);
});


9.

const car = {
  brand: "Tesla",
  model: "S",
  batteryLevel: 85,
  provideInfo() {
    return `This is a ${this.brand} ${this.model}, and the battery level is at ${this.batteryLevel}%.`;
  }
};

console.log(car.provideInfo());
// ์ถœ๋ ฅ: This is a Tesla S, and the battery level is at 85%.

 
 
 

Corner node.js 2ํŒ€

Editor : ํ‘ธ์ง๋ฐ”์˜ค

728x90

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ