Jieunny์˜ ๋ธ”๋กœ๊ทธ

Unit 9. [์‹ค์Šต]JavaScript Koans ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ณธ๋ฌธ

CodeStates/Training

Unit 9. [์‹ค์Šต]JavaScript Koans ๋ฌธ์ œ ํ•ด๊ฒฐ

Jieunny 2022. 12. 26. 13:11

๐Ÿ“ฃ  Jest : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ…Œ์ŠคํŠธ ํˆด

ํ…Œ์ŠคํŒ… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ƒํ™ฉ๋“ค์„ ์„ค์ •ํ•˜๊ณ , ๊ทธ ์ƒํ™ฉ์— ๋งž๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ์ž๋™์œผ๋กœ ํ…Œ์ŠคํŠธ ํ•œ๋‹ค.

 

๐Ÿ“ฃ  JavaScript Koans ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋ผ.

 

โœ”๏ธ expect 

 

โžฐ expect(n).toBe(๊ธฐ๋Œ“๊ฐ’) : n === ๊ธฐ๋Œ“๊ฐ’์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

โžฐ expect(n).toBeTruthy() : n === true ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

โžฐ expect(n).toEqual(๊ธฐ๋Œ“๊ฐ’) : n === ๊ธฐ๋Œ“๊ฐ’์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

 

๐Ÿšจ toBe() ์™€ toEqual() ์˜ ์ฐจ์ด์  

โžฐ toBe() ๋Š” ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. -> ๊ฐ์ฒด์˜ ๋‚ด์šฉ์ด ๊ฐ™๋”๋ผ๋„ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ์œผ๋ฉด False

โžฐ toEqual() ์€ ๊ฐ์ฒด์˜ ๋‚ด์šฉ์ด ๊ฐ™์€์ง€ ํ™•์ธํ•œ๋‹ค.

 

 

โœ”๏ธ type

 

โžฐ ๋Š์Šจํ•œ ๋™์น˜์—ฐ์‚ฐ์ž('==') ๋ง๊ณ  ์—„๊ฒฉํ•œ ๋™์น˜์—ฐ์‚ฐ์ž('===') ๋ฅผ ์‚ฌ์šฉํ•ด๋ผ.

โžฐ ์ตœ๋Œ€ํ•œ ๊ฐ™์€ ํƒ€์ž…๋ผ๋ฆฌ ์—ฐ์‚ฐํ•ด๋ผ.

โžฐ ์กฐ๊ฑด๋ฌธ์— ๋น„๊ต ์—ฐ์‚ฐ์„ ๋ช…์‹œํ•ด๋ผ.

 

 

โœ”๏ธ const

 

โžฐ const ๋ณ€์ˆ˜์—๋Š” ์žฌํ• ๋‹น์ด ๊ธˆ์ง€๋œ๋‹ค.

โžฐ const๋กœ ์„ ์–ธ๋œ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ, ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

โžฐ const๋กœ ์„ ์–ธ๋œ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ, ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿšจ ๊ทธ๋Ÿผ์—๋„ const๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

โžฐ ์˜๋„์น˜ ์•Š์€ ์žฌํ• ๋‹น์„ ๋ง‰์„ ์ˆ˜ ์žˆ์–ด ์•ˆ์ „ํ•˜๋‹ค.

 

 

โœ”๏ธ scope

 

โžฐ ํ˜ธ์ด์ŠคํŒ… 

์ฝ”๋“œ๊ฐ€ ์‹คํ–‰ํ•˜๊ธฐ ์ „ ๋ณ€์ˆ˜์„ ์–ธ/ํ•จ์ˆ˜์„ ์–ธ์ด ํ•ด๋‹น ์Šค์ฝ”ํ”„์˜ ์ตœ์ƒ๋‹จ์œผ๋กœ ๋Œ์–ด์˜ฌ๋ ค์ง„ ๊ฒƒ ๊ฐ™์€ ํ˜„์ƒ
์„ ์–ธ์ด ์ฝ”๋“œ ์‹คํ–‰๋ณด๋‹ค ๋จผ์ € ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋Š” ๊ณผ์ •์œผ๋กœ ์ธํ•œ ํ˜„์ƒ
-> ์ฝ”๋“œ ์‹คํ–‰ ์ „ ์ด๋ฏธ ์„ ์–ธ์ด ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„ ์–ธ๋ฌธ๋ณด๋‹ค ์ฐธ์กฐ/ํ˜ธ์ถœ์ด ๋จผ์ € ๋‚˜์™€๋„ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค.

 

โžฐ ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ์—๋Š” ํ˜ธ์ด์ŠคํŒ…์ด ์ผ์–ด๋‚˜์ง€๋งŒ, ํ•จ์ˆ˜ ํ‘œํ˜„์‹์—๋Š” ํ˜ธ์ด์ŠคํŒ…์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.

 

let funcExpressed = 'to be a function';

    expect(typeof funcDeclared).to.equal('function');
    expect(typeof funcExpressed).to.equal('string');	
    // ํ•จ์ˆ˜ ํ‘œํ˜„์‹์€ ํ˜ธ์ด์ŠคํŒ…์ด ์ผ์–ด๋‚˜์ง€ ์•Š์•„ ๋ฐ‘์— ์„ ์–ธ๋œ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ์œ„์— ์„ ์–ธ๋œ ๋ฌธ์ž์—ด์ด ์ธ์‹๋œ๋‹ค.

    function funcDeclared() {
      return 'this is a function declaration';
    }

    funcExpressed = function () {
      return 'this is a function expression';
    };
    

    const funcContainer = { func: funcExpressed };
    expect(funcContainer.func()).to.equal('this is a function expression');	
    // func ์—๋Š” funcExpressed๊ฐ€ ๋“ค์–ด์žˆ๊ณ  .func()์—์„œ funcExpressed ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์„œ ๋ฆฌํ„ด๊ฐ’์ธ ๋ฌธ์ž์—ด์ด ๋‚˜์˜จ๋‹ค.

    funcContainer.func = funcDeclared;
    expect(funcContainer.func()).to.equal('this is a function declaration');

 

โžฐ lexical scope (static scope๋ผ๊ณ ๋„ ํ•จ)

'ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ ํ˜ธ์ถœํ•˜์˜€๋Š”๊ฐ€'๊ฐ€ ์•„๋‹Œ 'ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ ์„ ์–ธํ•˜์˜€๋Š”๊ฐ€' ์— ๋”ฐ๋ผ scope๊ฐ€ ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ
ํ•จ์ˆ˜ foo๊ฐ€ bar์„ ํ˜ธ์ถœํ–ˆ๋‹ค๊ณ  bar์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„๊ฐ€ foo๊ฐ€ ์•„๋‹ˆ๋‹ค.
-> bar์˜ ์„ ์–ธ๋œ ์œ„์น˜๋ฅผ ๋ณด๋ฉด bar์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„๊ฐ€ ์ „์—ญ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ๋Š” ํ•˜์œ„ ์Šค์ฝ”ํ”„์˜ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— foo ํ•จ์ˆ˜์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜ x๋Š” bar ํ•จ์ˆ˜์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ๋ชปํ•œ๋‹ค.
var x = 'global x'

function foo() {	
  var x = 'local x'
  bar()
}

function bar() {
  console.log(x)
}

foo() // global x
bar() // global x

 

โžฐ lexical scope +  closure

    let age = 27;
    let name = 'jin';
    let height = 179;

    function outerFn() {
      let age = 24;
      name = 'jimin';
      let height = 178;

      function innerFn() {
        age = 26;
        let name = 'suga';
        return height;
      }

      innerFn();

      console.log(age);	// 26
      console.log(name);	// jimin

      return innerFn;
    }

    const innerFn = outerFn();

    console.log(age);	// 27
    console.log(name);	// jimin
    console.log(innerFn());	// 178
์ž์‹ ์˜ Scope -> ์ž์‹ ์„ ํฌํ•จํ•˜๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜ Scope -> ์ „์—ญ Scope ์ˆœ์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
์ „์—ญ์—๋„ ์—†์œผ๋ฉด ์—๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

ํด๋กœ์ €๋Š” ํ•จ์ˆ˜๋ฅผ ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„ ๋ฐ–์—์„œ ํ˜ธ์ถœํ•ด๋„ ํ•จ์ˆ˜๋Š” ์ž์‹ ์˜ ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„๋ฅผ ๊ธฐ์–ตํ•˜๊ณ  ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํŠน์„ฑ์ด ์žˆ๋‹ค.

 

 

โžฐ default parameter 

ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฃผ์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ default parameter๋กœ ์ž๋™์œผ๋กœ ์„ ์–ธ ๋œ๋‹ค.

 

โžฐ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜

const add = (x, y) => {
      return x + y
}
add(10, 20)	// 30
    
const subtract = (x, y) => x - y
subtract(50, 30)	// 20


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

 

โžฐ ์›์‹œ ์ž๋ฃŒํ˜•

ํ•จ์ˆ˜์˜ ์ „๋‹ฌ์ธ์ž๋„ ๋ณ€์ˆ˜์— data๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ
ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋„˜๊ธด ์ „๋‹ฌ์ธ์ž๋Š” ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์˜ ์ง€์—ญ๋ณ€์ˆ˜ -> ํ˜ธ์ถœ ์‹œ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ ์„ ์–ธ๋œ๋‹ค. (๋ณธ๋ž˜์˜ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š์Œ)

 

โžฐ ์ฐธ์กฐ ์ž๋ฃŒํ˜•

1๏ธโƒฃ ๋ฐฐ์—ด

const ages = [22, 23, 27];
allowedToDrink = ages;
allowedToDrink === ages;	// true
allowedToDrink === [22, 23, 27];	// false
๋ฐฐ์—ด์˜ ๋น„๊ต๋Š” ๊ฐ’์ด ์•„๋‹ˆ๋ผ ์ฃผ์†Œ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰์ด false ๊ฐ€ ๋‚˜์˜จ๋‹ค.
[22, 23, 27] ์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ heap์— ์ €์žฅ๋˜๊ณ  ages์—๋Š” ๊ทธ ์ฃผ์†Œ๊ฐ€ ์ €์žฅ๋œ๋‹ค.

 

2๏ธโƒฃ ๊ฐ์ฒด

const person = {
      son: {
        age: 9,
      },
    };
    
const boy = person.son;
// boy : { age: 9 };

boy.age = 20;
person.son.age	// 20 ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์–ด์„œ boy๋ฅผ ๋ฐ”๊พธ๋ฉด person๋„ ๋ฐ”๋€๋‹ค.
person.son === boy	// true ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ.
person.son === { age: 9 }	// false age:20
person.son === { age: 20 }	// false ๊ฐ’์ด ์•„๋‹ˆ๋ผ ์ฃผ์†Œ๋ฅผ ๋น„๊ตํ•˜๋Š”๊ฑฐ์ž„!!
    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',
      },
    };
    
    megalomaniac.length;	// undefined
๊ฐ์ฒด ๊ธธ์ด๋ฅผ ๊ตฌํ•  ๋•Œ๋Š” ๊ฐ์ฒด.length๊ฐ€ ์•„๋‹ˆ๋ผ Object.keys(๊ฐ์ฒด).length ์‚ฌ์šฉ
๐Ÿšจ ๊ฐ์ฒด.length ํ•˜๋ฉด undefined ๋‚˜์˜จ๋‹ค. ๊ฐ์ฒด์— length๋ž€ ํ‚ค๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ.

'this'๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์‹œ์ ์— ๊ฒฐ์ •๋œ๋‹ค.

Object.assign() ์„ ํ†ตํ•œ ๋ณต์‚ฌ๋Š” ์ฃผ์†Œ๋งŒ ๋ณต์‚ฌ๋œ๋‹ค. -> ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๊ฑด ์•„๋‹˜!
const obj = {
      mastermind: 'Joker',
      henchwoman: 'Harley',
      relations: ['Anarky', 'Duela Dent', 'Lucy'],
      twins: {
        'Jared Leto': 'Suicide Squad',
        'Joaquin Phoenix': 'Joker',
        'Heath Ledger': 'The Dark Knight',
        'Jack Nicholson': 'Tim Burton Batman',
      },
    };

    function passedByReference(refObj) {
      refObj.henchwoman = 'Adam West';
    }
    passedByReference(obj);
    obj.henchwoman	// 'Adam West'

    const assignedObj = obj;
    assignedObj['relations'] = [1, 2, 3];
    obj['relations'])	// [1, 2, 3]

    const copiedObj = Object.assign({}, obj);
    copiedObj.mastermind = 'James Wood';
    obj.mastermind	// 'Joker'

    obj.henchwoman = 'Harley';
    copiedObj.henchwoman	// 'Adam West'

    delete obj.twins['Jared Leto'];
    'Jared Leto' in copiedObj.twins	// false
์–•์€ ๋ณต์‚ฌ : ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๊ธฐ๋ณธํ˜• ๋ฐ์ดํ„ฐ๋Š” ๋ณต์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๋˜ ๋‹ค๋ฅธ ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ๋Š” ๋ณต์‚ฌ๊ฐ€ ์•„๋‹Œ ํ• ๋‹น์„ ํ•˜๋Š” ๊ฒƒ
assign, spread
์›์‹œ ๋ฐ์ดํ„ฐ๋Š” ๋ฐ”๊พผ ๊ฐ์ฒด๋งŒ ๋ฐ”๋€Œ์ง€๋งŒ, ์ฐธ์กฐ ๋ฐ์ดํ„ฐ๋Š” ๋‹ค๋ฅธ ๊ฒƒ๋„ ๋ฐ”๋€๋‹ค.
mastermind์™€ henchwoman์€ string ๋ฐ์ดํ„ฐ๋ผ์„œ ์ง์ ‘ ๋ฐ”๊พผ ๊ฐ์ฒด์˜ ๊ฐ’๋งŒ ๋ฐ”๋€Œ์ง€๋งŒ twins๋Š” object ๋ฐ์ดํ„ฐ๋ผ์„œ ํ•˜๋‚˜๋งŒ ๋ฐ”๊ฟ”๋„ ๋‚˜๋จธ์ง€๋„ ๋ฐ”๋€๋‹ค.

 

โžฐ Spread & Rest

...์„ ์–ด๋””์— ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋ƒ์— ๋”ฐ๋ผ spread syntax ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ  rest syntax๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๋‹ค.

1๏ธโƒฃ Spread

์š”์†Œ๋ฅผ ๋ชจ๋‘ ํ•˜๋‚˜์”ฉ ์ „๊ฐœ์‹œํ‚ฌ ๋•Œ

2๏ธโƒฃ Rest

๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ๋ฌถ์„ ๋•Œ
ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์“ฐ์—ฌ์„œ ๋’ค์— ๋‚จ๋Š” ์š”์†Œ๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ฐ›์•„์ค€๋‹ค.
๊ทธ๋ž˜์„œ Rest ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋งˆ์ง€๋ง‰์—๋งŒ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

3๏ธโƒฃ spread ๋„์ž… ์ „ ๊ฐ์ฒด๋กœ ํ•จ์ˆ˜ ์ „๋‹ฌ์ธ์ž ๋‹ค๋ฃจ๊ธฐ

function getAllParamsByArgumentsObj() {
      return arguments;
}
    
const argumentsObj = getAllParamsByArgumentsObj('first', 'second', 'third');
    
Object.keys(argumentsObj)	// ['0', '1', '2']
Object.values(argumentsObj)	// ['first', 'second', 'third']