Javieratapiab
8/20/2017 - 1:08 PM

Options object + named parameters

Options object + named parameters

// The option object is widely used pattern that allows user-defined settings 
// to be passed to a function in the form of properties on an object

setPageThread("New version out soon!", {
  popular: true,
  expires: 10000,
  activeClass: 'is-page-thread'
});

// Option object with 3 properties 

function setPageThread(name, { popular, expires, activeClass } = {}) {
  // It wouldn't raise an error because second parameters are optional
  console.log('name' + name);
  console.log('popular' + popular);
  console.log('expires' + expires);
  console.log('active' + activeClass);
  // ...
}

// another example

function loadProfiles(userNames = [], {profilesClass, reverseSort} = {}) {
  profilesClass = profilesClass || ".user-profile";
  reverseSort   = reverseSort   || false;

  if (reverseSort) {
    userNames = _reverse(userNames);
  }

  _loadProfilesToSideBar(userNames, profilesClass);
}