๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Frontend Dev/๐Ÿฅ ์ฝ”๋“œ์Šคํ…Œ์ด์ธ  FE ๋ถ€ํŠธ์บ ํ”„

Section1 Unit9 ๊ณผ์ œ JavaScript Koans

๋ฐ˜์‘ํ˜•


Section1 Unit9 [JavaScript] JavaScript Koans

 

โญ๏ธ ๊ณผ์ œ. JavaScript Koans

 ์ด๋ฒˆ ์œ ๋‹›์˜ JavaScript Koans ๊ณผ์ œ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…์„ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. Javascript Koans๋Š” 1๋ฒˆ ์†Œ๊ฐœ ์ฑ•ํ„ฐ๋ฅผ ์ œ์™ธํ•˜๊ณ , 10๋ฒˆ๊นŒ์ง€ 9๊ฐœ์˜ ์ฑ•ํ„ฐ๋กœ ๋‚˜๋ˆˆ ํŒŒ์ผ์ด ์ œ๊ณต๋˜์—ˆ๋‹ค. (ํƒ€์ž…, ๋ณ€์ˆ˜, ์Šค์ฝ”ํ”„, ํ•จ์ˆ˜, ๋ฐฐ์—ด, ๊ฐ์ฒด, ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ• ๋“ฑ)

 ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„๋“ค์€ ๋”ฐ๋กœ ์ •๋ฆฌํ•˜๊ณ , ๋ฌธ์ œ๋„ ์ตœ๋Œ€ํ•œ ๋‹ค ์ดํ•ดํ•˜๋ ค ํ–ˆ๋‹ค.

 ์ด๋ฒˆ TIL์—์„œ๋Š” ํ—ท๊ฐˆ๋ ธ๋˜ ๊ฐœ๋…์ด๋ž‘ ๋ฌธ์ œ๋“ค, ๋”ฐ๋กœ ์ฐพ์•„๋ณด๊ธฐ๋ฅผ ๊ถŒ๊ณ ํ–ˆ๋˜ ์‚ฌํ•ญ ์ค‘์— ๋‚ด๊ฐ€ ์ž˜ ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„๋“ค์„ ์œ„์ฃผ๋กœ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณผ๊นŒ ํ•œ๋‹ค ๐Ÿ™‚

 

 

• 03_LetConst.js

 

Q. ์žฌํ• ๋‹น๋„ ์•ˆ๋˜๋Š” 'const' ํ‚ค์›Œ๋“œ๋ฅผ ๊ตณ์ด ์จ์•ผํ•˜๋Š”์ง€ ์ดํ•ด๊ฐ€ ์•ˆ ๋ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 'let' ํ‚ค์›Œ๋“œ๋Š” ์žฌํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ๋ชจ๋กœ ํŽธํ•˜๊ณ , ํฐ ๋ฌธ์ œ๋„ ์—†์–ด ๋ณด์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ ์ž ์‹œ ๊ณ ๋ฏผํ•˜์‹  ํ›„, 'const'๊ฐ€ ์ถ”์ฒœ๋˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด ์ง์ ‘ ์ฐพ์•„๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

A. const ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์˜๋„์น˜ ์•Š์€ ์žฌํ• ๋‹น์„ ๋ฐฉ์ง€ํ•ด ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— let์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์•ˆ์ „ํ•˜๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”(์žฌํ• ๋‹น์ด ํ•„์š” ์—†๋Š” ์ƒ์ˆ˜) ์›์‹œ ๊ฐ’๊ณผ ๊ฐ์ฒด์—๋Š” const ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

↓ use const and let

 

Google JavaScript Style Guide

Google JavaScript Style Guide 1 Introduction This document serves as the complete definition of Google’s coding standards for source code in the JavaScript programming language. A JavaScript source file is described as being in Google Style if and only i

google.github.io

 

 

• 04_Scope.js

 

Q. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜ ํ˜ธ์ด์ŠคํŒ…(hoisting)์— ๋Œ€ํ•ด์„œ ๊ฒ€์ƒ‰ํ•ด ๋ด…๋‹ˆ๋‹ค.

A. ํ•จ์ˆ˜์˜ ํ˜ธ์ด์ŠคํŒ…์ด๋ž€ ํ•จ์ˆ˜ ์„ ์–ธ์˜ ์œ„์น˜์™€๋Š” ์ƒ๊ด€์—†์ด ์ฝ”๋“œ ๋‚ด ์–ด๋Š ๊ณณ์—์„œ๋“ ์ง€ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋กœ, ํ•จ์ˆ˜์„ ์–ธ๋ฌธ์€ ํ˜ธ์ด์ŠคํŒ…์ด ๋˜์ง€๋งŒ, ํ•จ์ˆ˜ํ‘œํ˜„์‹์€ ํ˜ธ์ด์ŠคํŒ…์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

Q. ํด๋กœ์ €์˜ ์œ ์ฆˆ ์ผ€์ด์Šค

 ๐Ÿ’ฌ ํด๋กœ์ €๋„ ์–ด๋ ค์šด๋ฐ ํด๋กœ์ €์˜ ์œ ์ฆˆ ์ผ€์ด์Šค๋ฅผ ์ฐพ์•„๋ณด๋ผ๋‹ˆ... ์•„์ง๊นŒ์ง€๋„ ํด๋กœ์ €๋ผ๋Š” ๊ฐœ๋…์ด ์‰ฝ์ง€ ์•Š์•„ ์œ ์ฆˆ ์ผ€์ด์Šค ํ•จ์ˆ˜ ํŒฉํ† ๋ฆฌ(function factories)์™€ namespacing(private variables/functions)๋ฅผ ์ฐพ์•„๋ณด๊ธฐ๋Š” ํ–ˆ๋Š”๋ฐ, ์ดํ•ด๊ฐ€ ๋‹ค ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค. ์ข€ ๋” ๊ณต๋ถ€ํ•ด์„œ ์ •๋ฆฌํ•ด๋ด์•ผ ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด, 

A. ํ•จ์ˆ˜ ํŒฉํ† ๋ฆฌ(function factories)๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋กœ, ํ•จ์ˆ˜ ํŒฉํ† ๋ฆฌ์—์„œ ๋ฐ˜ํ™˜๋œ ํ•จ์ˆ˜๋Š” ํด๋กœ์ €๋ฅผ ํ˜•์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ๋‹ค. namespacing์€ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜๋ฅผ ํ˜„์žฌ ์œ ํšจ ๋ฒ”์œ„ ๋‚ด์— ์ œํ•œํ•˜์—ฌ ์ด๋ฆ„ ์ถฉ๋Œ์„ ๋ฐฉ์ง€ํ•˜๊ณ  ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜๋ฅผ ์ˆจ๊ธฐ๋Š” ๊ฒƒ์„ ๋งํ•˜๋Š”๋ฐ, ํด๋กœ์ €๋กœ ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ ์„ ์ด์šฉํ•œ๋‹ค.

 ๐Ÿ’ฌ ์˜ˆ์‹œ ์ฝ”๋“œ๋„ ๋ดค๋Š”๋ฐ, ์•„์ง ์„ค๋ช…ํ•  ์ •๋„๋กœ ์ดํ•ดํ•œ ๊ฑด ์•„๋‹Œ๊ฒƒ ๊ฐ™์•„์„œ ๋” ๊ณต๋ถ€ํ•˜๊ณ  ์ •๋ฆฌํ•˜๊ธฐ๋กœ ๐Ÿ˜ญ

 

 

• 05_ArrowFunction.js

 

 • ↓ ํด๋กœ์ € + ํ™”์‚ดํ‘œ ํ•จ์ˆ˜

// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํด๋กœ์ €๋ฅผ ํ‘œํ˜„
const adder = x => {
 return y => {
  return x + y
 }
}

const subtractor = x => y => {
 return x - y
}

/*
์œ„ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์•„๋ž˜ ํ•จ์ˆ˜ ํ‘œํ˜„์‹๊ณผ ๊ฐ™์Œ 
const subtractor = function(x) {
 return function(y) {
  return x - y;
 }
}
→ ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’์ด ํ•จ์ˆ˜
*/

console.log( adder(50)(10) ); // 60
console.log( subtractor(50)(10) ); // 40

function makeTag() {
 const htmlMaker = tag => textContent => `<${tag}>${textContent}</${tag}>`
 console.log(htmlMaker('div')('code states')); // <div>code states</div>

 const liMaker = htmlMaker('li')
 console.log(liMaker('1st item')) // <li>1st item</li>
 console.log(liMaker('2nd item')) // <li>2nd item</li>
}

makeTag();

 ์ด๊ฑด ๊ฝค ์žฌ๋ฐŒ์—ˆ๋˜ ํ•จ์ˆ˜!

 

 

• 07_Array.js

 

๐Ÿ“Ž pass by value์™€ pass by reference์˜ ์ฐจ์ด๋Š” ์•„๋ž˜ ๊ธ€ ์ฐธ๊ณ  ๐Ÿ˜Š

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ž๋ฃŒํ˜• - ์›์‹œ ํƒ€์ž…(primitive type)๊ณผ ๊ฐ์ฒด ํƒ€์ž…(object type)์˜ ํŠน์ง•

๐Ÿ‘พ ์›์‹œ ํƒ€์ž…(primitive type) ๋ณ€์ˆ˜์— ํ• ๋‹น์‹œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ๊ฐ’ ์ž์ฒด๊ฐ€ ์ €์žฅ๋œ๋‹ค. ์›์‹œ ๊ฐ’์„ ๊ฐ–๋Š” ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ์›์‹œ ๊ฐ’ ์ž์ฒด๊ฐ€ ๋ณต์‚ฌ๋˜์–ด ์ „๋‹ฌ๋œ๋‹ค. (pass-by-value) → ์›๋ณธ์„ ๋ณ€๊ฒฝํ•ด๋„

fay-story.com

const arr = ['zero', 'one', 'two', 'three', 'four', 'five'];
// call(pass) by value์™€ call(pass) by reference์˜ ์ฐจ์ด์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค.

function passedByReference(refArr) {
  refArr[1] = 'changed in function';
}
passedByReference(arr);
console.log("arr", arr); // arr = ['zero', 'changed in function', 'two', 'three', 'four', 'five'];

const assignedArr = arr; 
assignedArr[5] = 'changed in assignedArr';
console.log("assignedArr", assignedArr); // assignedArr = ['zero', 'changed in function', 'two', 'three', 'four', 'changed in assignedArr'];
console.log("arr", arr) // arr = ['zero', 'changed in function', 'two', 'three', 'four', 'changed in assignedArr'];

const copiedArr = arr.slice();
copiedArr[3] = 'changed in copiedArr';
console.log("copiedArr", copiedArr); // copiedArr = ['zero', 'changed in function', 'two', 'changed in copiedArr', 'four', 'changed in assignedArr'];
console.log("arr", arr); // arr = ['zero', 'changed in function', 'two', 'three', 'four', 'changed in assignedArr'];

 

 

• 08_Object.js

 

const obj = {}
console.log(obj.length) // undefined


const megalomaniac = {
  mastermind: 'Joker',
  henchwoman: 'Harley',
  getMembers: function () {
    return [this.mastermind, this.henchwoman];
  },
  relations: ['Anarky', 'Duela Dent', 'Lucy'],
  twins: {
    'Jared Leto': 'Suicide Squad',
    'Joaquin Phoenix': 'Joker',
    'Heath Ledger': 'The Dark Knight',
    'Jack Nicholson': 'Tim Burton Batman',
  },
};

console.log(megalomaniac.length) // // undefined

→ ๊ฐ์ฒด๋Š” length ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋Š”๋‹ค. (์ƒ๊ฐํ•ด๋ณด์ง€ ์•Š์€ ๋ถ€๋ถ„์ธ๋ฐ, ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค..ใ…Žใ…Ž)

 

Q. this๋ž€ ๋ฌด์—‡์ผ๊นŒ?

A. this๋Š” ๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ํด๋ž˜์Šค์—์„œ ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ์œ„ํ•œ ํŠน์ˆ˜ํ•œ ๋ณ€์ˆ˜๋‹ค.

class Car { 
 constructor(brand) {
  this.brand = brand // ์—ฌ๊ธฐ์„œ this๋Š” car
 }
}

const car = new Car('Hyundai')

 

 

• 09_SpreadSyntax.js

 

const fullPre = {
  cohort: 7,
  duration: 4,
  mentor: 'hongsik',
};

const me = {
  time: '0156',
  status: 'sleepy',
  todos: ['coplit', 'koans'],
};

const merged = { ...fullPre, ...me };
// 1. ๋ณ€์ˆ˜ 'merged'์— ํ• ๋‹น๋œ ๊ฒƒ์€ 'obj1'๊ณผ 'obj2'์˜ value์ผ๊นŒ์š”, reference์ผ๊นŒ์š”? // value
// 2. ๋งŒ์•ฝ ๊ฐ’(value, ๋ฐ์ดํ„ฐ)์ด ๋ณต์‚ฌ๋œ ๊ฒƒ์ด๋ผ๋ฉด, shallow copy์ผ๊นŒ์š”, deep copy์ผ๊นŒ์š”? // shallow

console.log(merged)
/* merged
{
  cohort: 7,
  duration: 4,
  mentor: 'hongsik',
  time: '0156',
  status: 'sleepy',
  todos: [ 'coplit', 'koans' ]
}
*/

 → ์ด ๋ฌธ์ œ ์ง„์งœ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ, ๋ณ€์ˆ˜ merged์— ํ• ๋‹น๋œ ๊ฒƒ์€ value์ด๊ณ , ์ด ๊ฐ’์€ ์–•์€ ๋ณต์‚ฌ๊ฐ€ ๋œ ๊ฐ’์ด๋‹ค.

 

 • arguments ๊ฐ์ฒด์™€ rest parameter

 

๋””ํดํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜ (with arguments ๊ฐ์ฒด)

๋””ํดํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜(default parameter)์™€ ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜(rest parameter)๋Š” ECMAScript 6๋ถ€ํ„ฐ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์ด์ „ ๋ฒ„์ „์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ํ•จ์ˆ˜์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด arguments ๊ฐ์ฒด

fay-story.com

 ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ arguments ๊ฐ์ฒด์— ๊ด€ํ•œ ๊ธ€์€ ๋”ฐ๋กœ ์ •๋ฆฌ๋ฅผ ํ–ˆ๋‹ค. ↑ ์œ„ ๊ธ€ ์ฐธ๊ณ !

 


๐ŸŒ™  ์˜ค๋Š˜์˜ ํšŒ๊ณ 

 JavaScript Koans๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•˜๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…์„ ์ •๋ฆฌํ•˜๊ณ  ์ถ”๊ฐ€๋กœ ๋” ํ•„์š”ํ•œ ๋ถ€๋ถ„๋“ค์ด ๋ฌด์—‡์ธ์ง€๋ฅผ ํ•™์Šตํ–ˆ๋‹ค. JavaScript Koans์—์„œ ํ‘ผ ๋ฌธ์ œ๋“ค์ด ์ „๋ฐ˜์ ์œผ๋กœ ์žฌ๋ฏธ์žˆ์–ด์„œ ๋‚˜๋ฆ„ ์ •๋ฆฌ๋ฅผ ํ•˜๋ฉฐ ํ’€์ด๋ฅผ ํ–ˆ๊ณ , ๋ผ์ด๋ธŒ ์„ธ์…˜์—์„œ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ฌธ์ œ ํ’€์ด๋„ ๋“ค์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค. Koans๋Š” ๋ถˆ๊ต์—์„œ ์œ ๋ž˜๋œ ๋‹จ์–ด๋กœ ๊ฒฐ๋ก ์„ ๋‚ด๋ฆฌ๊ธฐ ์ „์— ์ด๊ฒŒ ์™œ ๋งž๋Š”์ง€ ๊นŠ๊ฒŒ ๊ณ ๋ฏผํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์ด๋ฒˆ ๋ฌธ์ œ๋“ค์€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์— ๋‹ต์ด ์ด๋ฏธ ์ œ์‹œ๊ฐ€ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ฌด ์ƒ๊ฐ์—†์ด ํ’€๋ฉด ๊ทธ๋ƒฅ ๋ฌผํ๋ฅด๋“ฏ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋“ค์ด์—ˆ๋Š”๋ฐ, ๋‹ต์„ ๊ณ ๋ฏผํ•ด๋ณด๋ฉฐ ๋‚ด๊ฐ€ ๋ชจ๋ฅด๋Š”๊ฒŒ ๋ฌด์—‡์ด๊ณ , ํ—ท๊ฐˆ๋ฆฌ๋Š”๊ฒŒ ๋ฌด์—‡์ธ์ง€ ํ™•์‹คํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์–ด์„œ ์˜๋ฏธ์žˆ๋Š” ์‹œ๊ฐ„์ด์—ˆ๋‹ค.

๋‹ค์‹œ ํ•œ ์ฃผ๊ฐ€ ์‹œ์ž‘๋˜์—ˆ๋‹ค. ์ด๋ฒˆ์ฃผ๋„ ํ™”์ดํŒ… ๐Ÿ‘ป

๋ฐ˜์‘ํ˜•