드림코딩 자바스크립트 기초강의 정리5

Dongwoo Kim·2021년 7월 16일
0

Arrow Function은 무엇인가? 함수의 선언과 표현

  1. 함수는 object이다 → 변수로 함수 사용가능, 변수에 할당가능 등 여러가지 일이 가능하게 함

    10:10 default parameters 기본 매개변수값 지정

    10:20 rest parameters ... 배열로 풀어줌 + for...of & forEach

    12:40 local scope → (확장) → closure, lexical scope 주요개념 : 밖에서는 안이 보이지 않고 안에서만 밖을 볼 수 있다!(변수 사용 범위)

    14:50 return 함수에 작성안하면 (return undefined) 가 생략된 채 작성된다. + early return : 여러개 검사 시 빨리 리턴을 해서 속도 up

    18:00 선언형 함수 ex) function sum() {..
    할당형 함수 ex) const print = function() {...
    선언형 함수는 완성되었을 때 제일 위로 올려진다. 그래서 선언 이전에 사용 가능함(호이스팅)

    18:34 callback function 매개변수가 함수인 경우를 의미

    23:17 IIFE Immediately Invoked Function Expression (function hello(){...})() 부르지 않아도 바로 실행가능

    Quiz time

    function calculate(cmd a, b)

    command(add, substract, divide, multifly, remainder)

function calculate(command, a, b) {
	if(command === 'add') return a+b;
	else if(command === 'substract') return a-b;
 	else if(command === 'multiply') return a*b;
 	else if(command === 'remainder') return a%b;
 	else return 'Wrong command!'
}

//정답 : command가 정해져 있다면 switch 문을 쓰는 것이 좋다.
function calculate(command, a, b) {
	switch (command) {
      case 'add':
        return a + b;
      case 'substract':
        return a - b;
      case 'multiply':
        return a * b;
      case 'remainder':
        return a % b;
      default:
        throw Error('unknown command');
    }
}
profile
水滴石穿

0개의 댓글