Skip to content

Instantly share code, notes, and snippets.

@bwreid

bwreid/01 ES6.md Secret

Created April 26, 2018 21:49
Show Gist options
  • Save bwreid/ae95005b2cc84a5f2649e763ea1db2b8 to your computer and use it in GitHub Desktop.
Save bwreid/ae95005b2cc84a5f2649e763ea1db2b8 to your computer and use it in GitHub Desktop.

ECMAScript 2015 / ES6

Objectives

  • Describe the purpose of Babel and when it should be used
  • Concatenate strings and variables with template strings
  • Declare variables with only let and const
  • Write anonymous functions with arrow functions
  • Declare defaults to functions parameters
  • Declare object literals with new shorthand
  • Destructure arrays and objects to assign values
  • Write dynamic code with the rest and spread operators

Exit Ticket

  • Update the following code to use ES6 features:

    var winners = [ 'Otis Duncan', 'Melba Frank', 'Damon Adams' ]
    
    function printWinnersByPlace (people) {
      var first = winners[0]
      var rest = winners.slice(1)
      return 'In 1st place is ' + first + '! Followed up by ' + rest.join(' and ') + '.'
    }
    
    printWinnersByPlace(winners)
  • Update the following code to be terser and use ES6 features:

    function orderBy (array, key, options) {
      options = options || {}
      var toSort = array.slice()
    
      if (options.asc) {
        toSort.sort(function (a, b) {
          return a[key] > b[key] ? 1 : -1
        })
      }
    
      if (options.desc) {
        toSort.sort(function (a, b) {
          return a[key] < b[key] ? 1 : -1
        })
      }
    
      return toSort
    }
    
    const people = [
      { firstName: 'Dustin', lastName: 'French', age: 23 },
      { firstName: 'Norman', lastName: 'Sanders', age: 44 },
      { firstName: 'Jessica', lastName: 'Glover', age: 39 },
      { firstName: 'Alfred', lastName: 'Hicks', age: 31 }
    ]
    
    orderBy(people, 'lastName', { desc: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment