Callback , IIFE

Callback ?

// Callback function using function expression
function randomQuiz(answer, printYes, printNo){
  if(answer ==='love you'){
    printYes();
  } else{
    printNo();
  }
}

//anonymous function
const printYes = function() {
  console.log('yes!');
};

// named function
// better debugging in debugger's stack traces
// or, for recursions
const printNo = function print(){
  console.log('no!');
  print();
};

randomQuiz('wrong',printYes,printNo);
randomQuiz('love you',printYes,printNo);

IIFE (Immediately Invoked Function Expression)

  • 함수를 실행과 동시에 바로 동작시키고 싶을때 사용
(function hello(){
  console.log('IIFE');
})();