20201015, 20201016 TIL

2020. 10. 19. 09:39ยท ๐Ÿ”ฅTIL

20201015 TIL

// for..of ๋ฐฐ์—ด์„ ๋งŒ๋“ค ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ 
const numbers = [10, 20, 30, 40, 50];

for (let number of numbers){
  console.log(number);
}

for (let i = 0; i < numbers.length; i++){
  console.log(numbers[i]);
}

const doggy = {
  name : '๋ฉ๋ฉ์ด',
  sound: '๋ฉ๋ฉ',
  age : 2
};

console.log(Object.keys(doggy));
console.log(Object.values(doggy)); // ๊ฐ์ฒด์˜ value๊ฐ’๋“ค์„ ๋ณด์—ฌ์คŒ 
console.log(Object.keys(doggy)); // ๊ฐ์ฒด์˜ key๊ฐ’๋“ค์„ ๋ณด์—ฌ์คŒ.

for (let key in doggy){
  console.log(key); // key๊ฐ’๋“ค์ด ๋‹ค ๋‚˜ํƒ€๋‚˜์คŒ.
}

// key์™€ value๋ฅผ ํ•œ๋ฒˆ์— ๋ณด๋ ค๋ฉด?
// Template Literal
for(let key in doggy){
  console.log(`${key} : ${doggy[key]}`);
}
const superheroes = ['์•„์ด์–ธ๋งจ', '์บกํ‹ด ์•„๋ฉ”๋ฆฌ์นด', 'ํ† ๋ฅด', '๋‹ฅํ„ฐ ์ŠคํŠธ๋ ˆ์ธ์ง€'];

for (let i = 0; i < superheroes.length; i++){
  console.log(superheroes[i]);
}

function print(hero){
  console.log(hero);
}
superheroes.forEach(print)

// ์ด๋ ‡๊ฒŒ ์ค„์—ฌ์„œ ์“ฐ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. 
superheroes.forEach(function(hero){
  console.log(hero);
})

// ์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅ. 
superheroes.forEach(hero => {
  console.log(hero);
})
// map ํ•จ์ˆ˜
// ๋ฐฐ์—ด ์•ˆ์˜ ์›์†Œ๋ฅผ ๋ณ€ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

// ๊ฐ€๋ น ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.
const array = [1, 2, 3, 4, 5, 6, 7, 8];

// --------------------------------------------------------------------
// ๋ฐฐ์—ด ์•ˆ์˜ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ์ œ๊ณฑํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด?
// const squared = [];

// ๋ฐฉ๋ฒ• 1
// for (let i = 0; i < array.length; i++){
//   squared.push(array[i] * array[i]);
// }
// console.log(squared);

// ๋ฐฉ๋ฒ• 2
// forEach๋กœ๋„ ๊ตฌํ˜„ ๊ฐ€๋Šฅ.
// array.forEach((n) => {
//   squared.push(n * n);
// });
// console.log(squared);

// --------------------------------------------------------------------

// ๋ฐฉ๋ฒ• 3
// map์œผ๋กœ ๊ตฌํ˜„
const square = (n) => n * n;
const squared = array.map(square);
console.log(squared);

// ์ด๋ ‡๊ฒŒ๋„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
const squared2 = array.map((n) => n * n);
console.log(squared2);

// --------------------------------------------------------------------

// ๊ฐ์ฒด ๋ฐฐ์—ด ์„ ์–ธ
const items = [
  {
    id: 1,
    text: 'hello'
  },
  {
    id: 2,
    text: 'bye'
  }
];

// ์ด ๊ฐ์ฒด ๋ฐฐ์—ด๋“ค์„ text๋กœ๋งŒ ์ด๋ค„์ง„ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด?
const texts = items.map((item) => item.text);
console.log(texts);

// --------------------------------------------------------------------
// ์›ํ•˜๋Š” ํ•ญ๋ชฉ์ด ์–ด๋””์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋Š” ํ•จ์ˆ˜
// indexOf
const superheroes = ['์•„์ด์–ธ๋งจ', '์บกํ‹ด ์•„๋ฉ”๋ฆฌ์นด', 'ํ† ๋ฅด', '๋‹ฅํ„ฐ ์ŠคํŠธ๋ ˆ์ธ์ง€'];
const index = superheroes.indexOf('ํ† ๋ฅด');
console.log(index);

// ๋‹ค๋ฅธ ์˜ˆ์‹œ (๊ฐ์ฒด๋กœ ์ด๋ค„์ง„ ๋ฐฐ์—ด์ผ ๊ฒฝ์šฐ)
const todos = [
  {
    id: 1,
    text: '์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž…๋ฌธ',
    done: true
  },
  {
    id: 2,
    text: 'ํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: true
  },
  {
    id: 3,
    text: '๋ฐฐ์—ด ๋‚ด์žฅํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: false
  }
];

// ์ด ๋ฐฐ์—ด์—์„œ id๊ฐ€ 3์ธ ๊ฒƒ์„ ์ฐพ๊ณ  ์‹ถ๋‹ค๋ฉด?
const index2 = todos.indexOf(3);
console.log(index2); // -1์€ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์—†์Œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

// ๋”ฐ๋ผ์„œ id๊ฐ’์ด 3์ธ๊ฒƒ์„ ์ฐพ๊ณ  ์‹ถ๋‹ค๋ฉด findIndex()๋ฅผ ์จ์•ผ ํ•œ๋‹ค.
const index3 = todos.findIndex((todo) => todo.id === 3);
console.log(index3);

// find()ํ•จ์ˆ˜๋„ ์กด์žฌํ•œ๋‹ค.
// ํ•ด๋‹น ์›์†Œ๋ฅผ ๊ทธ๋ƒฅ ๊ฐ€์ ธ์˜จ๋‹ค.
const todo = todos.find((todo) => todo.id === 3);
console.log(todo);

// ์กฐ๊ฑด์„ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค.
const todo2 = todos.find((todo) => todo.done === false);
console.log(todo2);


// indexOf, find, findIndex๋Š” ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ๋กœ ์ฐพ์€ ํ•ญ๋ชฉ๋งŒ ์•Œ๋ ค์ค€๋‹ค. 
// indexOf : ํŠน์ • ๊ฐ’์ด๋ž‘ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์„ ์ฐพ์„ ๋•Œ 
// findIndex : findIndex๋‚ด๋ถ€์— ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด ํŠน์ • ๊ฐ’์„ ์กฐ๊ฑด์œผ๋กœ ์ฐพ์•„์„œ ๋ช‡๋ฒˆ์งธ์ธ์ง€ ์ฐพ์•„์คŒ
// find : find๋Š” ์œ„์—์ฒ˜๋Ÿผ ์ฐพ์€ ๊ฐ’ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
// filter
// ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์›์†Œ๋“ค์„ ์ฐพ์•„์„œ ๊ทธ ์›์†Œ๋“ค์„ ๊ฐ€์ง€๊ณ  ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฒƒ.

const todos = [
  {
    id: 1,
    text: '์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž…๋ฌธ',
    done: true
  },
  {
    id: 2,
    text: 'ํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: true
  },
  {
    id: 3,
    text: '๋ฐฐ์—ด ๋‚ด์žฅํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: false
  }
];

// ๊ธฐ์กด์˜ ๋ฐฐ์—ด์„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค.
// const tasksNotDone = todos.filter((todo) => todo.done === false);
const tasksNotDone = todos.filter((todo) => !todo.done); // done๊ฐ’์ด false์ธ๊ฒŒ ๋‚˜ํƒ€๋‚จ.
console.log(tasksNotDone);

// --------------------------------------------------------------------

// splice
// ๋ฐฐ์—ด์—์„œ ํŠน์ • ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ
// ์ œ๊ฑฐ ๊ณผ์ •์—์„œ ํ•ด๋‹น ์›์†Œ๊ฐ€ ๋ช‡๋ฒˆ์งธ์ธ์ง€ ๋ช…์‹œํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
const numbers = [10, 20, 30, 40];
const index = numbers.indexOf(30);
console.log(index);

// ์ด๋•Œ, splice()์˜ ๊ฒฐ๊ณผ๊ฐ’์€ ์ œ๊ฑฐํ•ด๋‚ธ ๋ฐฐ์—ด๋“ค์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
const spliced = numbers.splice(index, 2); // index๋ถ€ํ„ฐ ๋ช‡๊ฐœ๋ฅผ ์ง€์šฐ๊ฒ ๋‹ค. 2์ด๋ผ ํ•˜๋ฉด ๋‘๊ฐœ๋ฅผ ์ง€์šฐ๊ฒ ๋‹ค๋Š” ๊ฒƒ.
console.log(numbers);
console.log(spliced);

// --------------------------------------------------------------------
console.log('-----------------------------------------');
// slice
// splice๋Š” ๋ฐฐ์—ด์„ ์ž˜๋ผ๋‚ผ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
// ๋‹ค๋งŒ slice๋Š” ๊ธฐ์กด์˜ ๋ฐฐ์—ด์„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์œผ๋ฉฐ ํŒจ๋Ÿฌ๋ฏธํ„ฐ์— ๋„ฃ๋Š” ๊ฐ’๋„ ๋‹ค๋ฅด๋‹ค.

const numbers2 = [10, 20, 30, 40];

const sliced = numbers2.slice(0, 2); // ๋ช‡๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ช‡๋ฒˆ ์ธ๋ฑ์Šค ์ „๊นŒ์ง€ ์ž๋ฅผ์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค. 
console.log(sliced);
console.log(numbers2);
  • ํšŒ์‹ํ•˜๊ณ  ๋ป—์–ด์„œ 16์ผ์— ๊ธ€์„ ์˜ฌ๋ฆฌ์ง€ ๋ชปํ–ˆ๋‹ค.. ๊ทธ๋ž˜๋„ ๋Œ€ํ‘œ๋‹˜์ด ์ •๋ง ๋ง›์žˆ๋Š” ๊ณ ๊ธฐ๋ฅผ ์‚ฌ์ฃผ์…จ๋‹คใ…Žใ…Ž

20201016 TIL

// shift, pop, unshift, push

// shift
// ์ฒซ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋ฐฐ์—ด์—์„œ ์ถ”์ถœํ•ด์ค€๋‹ค.
const numbers = [10, 20, 30, 40];

const value = numbers.shift();
console.log(value);

// shift๋Š” ๊ธฐ์กด์˜ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ธฐ์กด ์ฒซ๋ฒˆ์งธ ์›์†Œ์ธ 10์ด ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค. 
console.log(numbers);


// pop 
// ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ๋ฐฐ์—ด์—์„œ ์ถ”์ถœํ•ด์ค€๋‹ค.
const numbers2 = [10, 20, 30, 40];
const value2 = numbers2.pop();
console.log(value2);
console.log(numbers2); 


// unshift
const numbers3 = [10, 20, 30, 40];
numbers3.unshift(5);
// numbers3 ๋งจ ์•ž๋ถ€๋ถ„์— ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค.
console.log(numbers3);

// push(), pop() & unshift(), shift()๊ฐ€ ์ง์„ ์ด๋ฃฌ๋‹ค. 
// ๊ฐ๊ฐ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์™€ ๋งˆ์ง€๋ง‰ ์š”์†Œ์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค. 
// ๋„ท๋‹ค ๋ฐฐ์—ด์— ์ง์ ‘์ ์œผ๋กœ ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค. 

// --------------------------------------------------

// concat()
// ์—ฌ๋Ÿฌ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ํ•ฉ์ณ์ค€๋‹ค. 
// ๊ทผ๋ฐ concat์€ ๊ธฐ์กด์˜ ๋ฐฐ์—ด์„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];

const concated = arr1.concat(arr2);
console.log(concated);


// es6์˜ spread์—ฐ์‚ฐ์ž 
const concated2 = [...arr1, ...arr2];
console.log(concated2);


// join()
// ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ๊ฐ’๋“ค์„ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ํ•ฉ์ณ์ค„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 
const array = [1, 2, 3, 4, 5];
console.log(array.join()); // ํ•ด๋‹น ๋ฐฐ์—ด์„ ์‰ผํ‘œ ์‚ฌ์ด์‚ฌ์ด์— ๋„ฃ์–ด์„œ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์คŒ. 

// ํŒจ๋Ÿฌ๋ฏธํ„ฐ ์•ˆ์—๋Š” separator์ž„. 
console.log(array.join(" --- "));
// reduce()
// ์ฃผ๋กœ ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ฐฐ์—ด ์•ˆ์˜ ๋ชจ๋“  ๊ฐ’๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค ๊ฐ’์„ ์—ฐ์‚ฐํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 
const numbers = [1, 2, 3, 4, 5];

// ๋ชจ๋“  ๋ฐฐ์—ด์˜ ์›์†Œ๋“ค์„ ํ•ฉํ•ด์„œ ์ดํ•ฉ์„ ๊ตฌํ•ด๋ณด์ž. 
// ๋ฐฉ๋ฒ• 1
let sum = 0;
numbers.forEach(n => {
  sum += n;
})

console.log(sum);

// ๋ฐฉ๋ฒ• 2 (reduceํ•จ์ˆ˜ ์‚ฌ์šฉ)
// accumulator ๋Š” ๋ˆ„์ ๋œ ๊ฐ’์„ ์˜๋ฏธํ•จ. 
// current๋Š” ์ฒ˜์Œ์— 1์ด ๋“ค์–ด๊ฐ€๊ณ  ๊ทธ ๋‹ค์Œ์— ์ฐจ์ฐจ ๋‹ค์Œ ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค. 
// ์ฒซ๋ฒˆ์งธ ํŒจ๋Ÿฌ๋ฏธํ„ฐ์—๋Š” ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ. 
const sum2 = numbers.reduce((accumulator, current) => accumulator + current, 0);
console.log(sum2);

// index๋Š” reduce์—์„œ ์ด ํ•จ์ˆ˜๊ฐ€ ๊ฐ ์›์†Œ์— ๋Œ€ํ•˜์—ฌ ์‹คํ–‰์ด ๋ ํ…๋ฐ 
// ์ด๋•Œ index๋Š” ๊ฐ ์›์†Œ๊ฐ€ ๋ช‡๋ฒˆ์งธ ์•„์ดํ…œ์ธ์ง€ ์•Œ๋ ค์ค€๋‹ค. ๊ทธ๋Ÿผ 0, 1, 2, 3, 4๊นŒ์ง€ ์‹คํ–‰๋  ๊ฒƒ. 
// array๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์žˆ๋Š” ์ž๊ธฐ ์ž์‹ ์„ ์˜๋ฏธํ•œ๋‹ค. 
const avg = numbers.reduce((accumulator, current, index, array) => {
  if(index === array.length - 1){
    // ํ˜„์žฌ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด ์›์†Œ๊ฐ€ ๋งจ ๋งˆ์ง€๋ง‰๊บผ๋ผ๋ฉด ... (-1์€ ์ธ๋ฑ์Šค๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ)
    // ๊ทธ๋ž˜์„œ ์ธ๋ฑ์Šค๊ฐ€ 4๋ผ๋ฉด..(๋งˆ์ง€๋ง‰ ์›์†Œ๋ผ๋ฉด) 
    return (accumulator + current) / array.length;
  }

  return accumulator + current;
}, 0);
console.log(avg);


// reduce()์˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ 
const alphabets = ['a', 'a', 'a', 'b', 'c', 'c', 'd', 'e'];

// ๊ฐ ์•ŒํŒŒ๋ฒณ์ด ๋ช‡๊ฐœ์”ฉ ์žˆ๋Š”์ง€ ์„ธ์„œ ๊ฐ์ฒด์— ๊ฒฐ๊ณผ๋ฅผ ๋„ฃ์–ด์ค„ ๊ฒƒ.
const counts = alphabets.reduce((acc, current) => {
  // ์—ฌ๊ธด ์ฝ”๋“œ๋ธ”๋Ÿญ์ด๋‹ค. ๋ฐ‘์— ๊ฐ์ฒด๋กœ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ค€ ๊ฒƒ๊ณผ ํ—ท๊ฐˆ๋ฆฌ๋ฉด ์•ˆ๋œ๋‹ค. 
  if (acc[current]){
    // ๋งŒ์•ฝ acc์— current๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด?
    acc[current] += 1;
  } else {
    acc[current] = 1;
  }
  return acc;


}, {}) // ๋น„์–ด์žˆ๋Š” ๊ฐ์ฒด๋กœ acc๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์˜€๋‹ค. 

console.log(counts);


// ํ”„๋กœํ† ํƒ€์ž…๊ณผ ํด๋ž˜์Šค - ๊ฐ์ฒด ์ƒ์„ฑ์ž 

// ๊ฐ์ฒด ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค ๋• ์ฃผ๋กœ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ 
function Animal(type, name, sound){
  this.type = type;
  this.name = name;
  this.sound = sound;


  // this.say = function(){
  //   console.log(this.sound);
  // }
}

// ํ”„๋กœํ† ํƒ€์ž… ์‚ฌ์šฉํ•˜๊ธฐ 
Animal.prototype.say = function(){
  console.log(this.sound);
}

const dog = new Animal('๊ฐœ', '๋ฉ๋ฉ์ด', '๋ฉ๋ฉ');

const cat = new Animal('๊ณ ์–‘์ด', '๋ƒ์˜น์ด', '๋ƒ์˜น');


dog.say();
cat.say();

// ๊ฐ’์„ ์žฌ์‚ฌ์šฉํ•˜๊ณ ์‹ถ๋‹ค!
Animal.prototype.sharedValue = 1;
console.log(dog.sharedValue);
console.log(cat.sharedValue);

// ํ”„๋กœํ† ํƒ€์ž…์˜ ์—ญํ• ์ด๋ž€..
// ๊ฐ์ฒด ์ƒ์„ฑ์ž๋กœ ๋ญ”๊ฐˆ ๋งŒ๋“ค์—ˆ์„ ๋•Œ ๊ทธ๊ฑธ ๋งŒ๋“  ๊ฐ์ฒด๋“ค๋ผ๋ฆฌ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋– ํ•œ ๊ฐ’์ด๋‚˜ ํ•จ์ˆ˜๋ฅผ 
// ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ์ƒ์„ฑ์ž๋กœ ๋งŒ๋“  ํ•จ์ˆ˜์— ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.  


// ํ”„๋กœํ† ํƒ€์ž…๊ณผ ํด๋ž˜์Šค - ๊ฐ์ฒด ์ƒ์„ฑ์ž 

// ๊ฐ์ฒด ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค ๋• ์ฃผ๋กœ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ 
function Animal(type, name, sound){
  this.type = type;
  this.name = name;
  this.sound = sound;
}

// ํ”„๋กœํ† ํƒ€์ž… ์‚ฌ์šฉํ•˜๊ธฐ 
Animal.prototype.say = function(){
  console.log(this.sound);
}

// Animal์ด ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ์žฌ์‚ฌ์šฉํ•˜์—ฌ Dog, Cat ๊ฐ์ฒด์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž.

function Dog(name, sound){
  Animal.call(this, '๊ฐœ', name, sound);
}

function Cat(name, sound){
  Animal.call(this, '๊ณ ์–‘์ด', name, sound);
}

Dog.prototype = Animal.prototype;
Cat.prototype = Animal.prototype;


// Dog, Cat์ด๋ผ๋Š” ๊ฐ์ฒด์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. 
const dog = new Dog('๋ฉ๋ฉ์ด', '๋ฉ๋ฉ');
const cat = new Cat('๋ƒ์˜น์ด', '๋ƒ์˜น');


dog.say();
cat.say();


์œ„์˜ ์ฝ”๋“œ๋ฅผ class๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค์‹œ ํ•ด๋ณด์ž.

// JS์—์„œ๋Š” ํด๋ž˜์Šค๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์—†์—ˆ๋‹ค.
// ํด๋ž˜์Šค๋Š” C++, C#, Java, PHP์—๋Š” ์žˆ์ง€๋งŒ... ๋น„์Šทํ•œ ์ž‘์—…์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํ•ญ์ƒ ํ•จ์ˆ˜๋ฅผ ์จ์™”์—ˆ๋‹ค.
// ๊ทผ๋ฐ es6์—์„œ class๋ฌธ๋ฒ•์ด ๋„์ž…๋˜์—ˆ๋Š”๋ฐ, ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ์˜ class์™€ ๋˜‘๊ฐ™์€ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ์—ฌํƒœ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด์˜จ ๊ฒƒ์„
// ๊ฐ„๋žตํ•˜๊ณ  ์•Œ๊ธฐ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

class Animal {
  constructor(type, name, sound) {
    this.type = type;
    this.name = name;
    this.sound = sound;
  }

  // say๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ๊ตฌํ˜„ํ•˜์˜€๋Š”๋ฐ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ฉด ์ž๋™์œผ๋กœ prototype์œผ๋กœ ๋“ฑ๋ก์ด ๋œ๋‹ค.
  say() {
    console.log(this.sound);
  }
}

console.log(Animal.prototype.say); // ํ•จ์ˆ˜๊ฐ€ ์„ค์ •๋˜์–ด์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

const dog = new Animal('๊ฐœ', '๋ฉ๋ฉ์ด', '๋ฉ๋ฉ');
const cat = new Animal('๊ณ ์–‘์ด', '์•ผ์˜น์ด', '์•ผ์˜น');

dog.say();
cat.say();

// ๊ทธ๋ฆฌ๊ณ  class๋ฅผ ์“ฐ๋ฉด ์ƒ์†ํ•  ๋•Œ ๋”์šฑ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

class Dog extends Animal {
  // ๊ธฐ์กด animal์—์„œ ์‚ฌ์šฉํ•˜๋Š” constructor๋ฅผ ๋ฎ์–ด ์“ธ ๊ฒƒ.
  // animal์ด ๊ฐ€์ง€๋Š” constructor๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ•˜๊ณ ๋‚˜์„œ ์ž์‹ ์ด ํ•ด์•ผํ•  ์ผ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  constructor(name, sound) {
    super('๊ฐœ', name, sound);
  }
}

class Cat extends Animal{
  constructor(name, sound){
    super('๊ณ ์–‘์ด', name, sound);
  }
}


const dog2 = new Dog('๋ฉ๋ฉ์ด', '๋ฉ๋ฉ');
const cat2 = new Cat('๊ณ ์–‘์ด', '์•ผ์˜น');
const cat3 = new Cat('์•ผ์˜ค์˜ค์˜ค์˜น์ด', '์•ผ์˜ค์˜ค์˜ค์˜ค์˜น');

dog2.say();
cat2.say();
cat3.say();



์ธ๊ฐ•๋“ค์œผ๋ฉด์„œ ๋”ฐ๋ผ ์นœ๊ฑธ ๊ทธ๋Œ€๋กœ ์˜ฌ๋ ธ๋„ค ใ…œ
๋งŒ๋“ค์–ด๊ฐ€๋ฉด์„œ ๊ณต๋ถ€ํ•ด์•ผ ์žฌ๋ฐ‹์ง€ ๊ณต๋ถ€๊ณต๋ถ€๊ณต๋ถ€ํ•˜๋ ค๋‹ˆ ์žฌ๋ฏธ๊ฐ€ ์—†๋‹ค...

๋ฐ˜์‘ํ˜•
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'๐Ÿ”ฅTIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

20201014 TIL  (0) 2020.10.14
20201013 TIL  (0) 2020.10.13
20201012 TIL  (0) 2020.10.12
2020.03.18.TIL  (0) 2020.03.18
2020.03.16.TIL  (0) 2020.03.17
'๐Ÿ”ฅTIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • 20201014 TIL
  • 20201013 TIL
  • 20201012 TIL
  • 2020.03.18.TIL
sovelop
sovelop
๋ฌด์Šจ ์ƒ๊ฐ์„ ํ•ด.. ๊ทธ๋ƒฅ ํ•˜๋Š”๊ฑฐ์ง€
sovelop
so's devlog
sovelop
์ „์ฒด
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (141)
    • ๐Ÿ”ฅTIL (15)
    • ์ƒ๊ฐ (5)
      • Daily Routine (0)
    • WEB (2)
    • VueJS (1)
    • ํŒŒ์ด์ฌ ๋ฌธ๋ฒ• (17)
      • Django (0)
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (23)
      • ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (13)
      • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (0)
      • ๊ธฐํƒ€ ์‚ฌ์ดํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (6)
    • ์ปดํ“จํ„ฐ๊ณตํ•™์ž…๋ฌธ (13)
    • Data_Analysis (9)
    • Javascript (8)
      • ๋ฌธ๋ฒ• (8)
      • node.js (0)
    • Java (9)
      • ๋ฌธ๋ฒ• (3)
      • Android Studio (0)
      • Algorithm (2)
    • Server (6)
      • sql (2)
      • linux (2)
    • Back-up (22)
      • Git + Github (5)
      • English (0)
      • etc (17)
    • ํ…Œํฌ ๊ด€๋ จ ์„ธ๋ฏธ๋‚˜ (4)
    • English (0)
    • Error (4)
    • ์ฝ”ํ…Œํ›„๊ธฐ (0)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • About me

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ์ฝ”๋”ฉ์ข€์•Œ๋ ค์ฃผ๋ผ
  • ํ˜ผ๊ณต๋‹จ
  • ์ฝ”์•Œ๋ผuniv
  • ๋ฌด์ ‘์ ์ €์†Œ์Œ
  • ํ˜ผ๊ณต์ž
  • # ๋ฐฑ์ค€ #ํŒŒ์ด์ฌ #python
  • va87m
  • ํ•œ๋น›๋ฏธ๋””์–ด

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

hELLO ยท Designed By ์ •์ƒ์šฐ.v4.2.2
sovelop
20201015, 20201016 TIL
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.