본문 바로가기

카테고리 없음

자바스크립트 패턴

var musicPlayer = function () {
  var songList = ['California Girls', 'California Dreaming', 'Hotel California'];  

  // We'll expose all these functions to the user
  function play () {
    console.log('Im playing the next song!');
  }

  function pause () {
    console.log('Im paused!');
  }

  function addTrackToMusicQueue (track) {
    songList.push(track);
    console.log('I added a song');
  }

  function showNextTrack () {
    console.log('My next track is', songList[0]);
  }

  // Let's hide this function
  function loadSong() {
    filesystem.loadNextSong();
  }

  return {
    playMusic: play,
    pauseMusic: pause,
    showNextTrack: showNextTrack,
    addTrack: addTrackToMusicQueue
  }
}

const musicModudle = musicPlayer();
musicModudle.playMusic();

musicModudle.songList.push("Rabbit");

Revealing Pattern

private이 없는 자바스크립트에서 private함수나 변수를 만들 때 사용된다. 클로저와 연관.