Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to ES2015

Intro to ES2015

Scott Doxey

August 17, 2016
Tweet

More Decks by Scott Doxey

Other Decks in Programming

Transcript

  1. Variable Scope let name = 'Scott'; { let name =

    'Scott Prime'; } console.log(name); / / Scott
  2. Variable Scope for (let i = 0; i < 10;

    i++) { console.log(i); } console.log(i); / / ReferenceError
  3. Variable Scope for (let i = 0; i < 10;

    i++) { setTimeout(function () { console.log(i); }, 1); }
  4. New function() syntax const drawRect = (context) => { context.save();

    context.fillStyle = '#E9434F'; context.fillRect(0, 0, 100, 100); context.restore(); } drawRect(canvas.getContext(‘2d'));
  5. New function() syntax const drawRect = (context) => { context.save();

    context.fillStyle = '#E9434F'; context.fillRect(0, 0, 100, 100); context.restore(); } drawRect(canvas.getContext(‘2d'));
  6. Default function values const logMessage = (content = 'No message

    supplied.') => { const message = `${Date.now()} – ${content}`; console.log(message); return message; } logMessage(); / / 1471307510395 – No message supplied. logMessage('Oh hai!'); / / 1471307510398 – Oh hai!
  7. Default function values const logMessage = (content = 'No message

    supplied.') => { const message = `${Date.now()} – ${content}`; console.log(message); return message; } logMessage(); / / 1471307510395 – No message supplied. logMessage('Oh hai!'); / / 1471307510398 – Oh hai!
  8. Classes class Game { constructor () { console.log('New game object!');

    } play () { console.log('Let\s do this!'); } } const snake = new Game(); / / New game object! snake.play(); / / Let’s do this!
  9. Destructing arrays & objects const [a, b] = [1, 2];

    console.log(`${a}, ${b}`); / / 1, 2
  10. Destructing arrays & objects const [a, b] = [1, 2];

    console.log(`${a}, ${b}`); / / 1, 2
  11. Destructing arrays & objects const {first, last} = {first: 'Scott',

    last: 'Doxey'}; console.log(`${last}, ${first} ${last}`); / / Doxey, Scott Doxey
  12. Destructing arrays & objects const {first, last} = {first: 'Scott',

    last: 'Doxey'}; console.log(`${last}, ${first} ${last}`); / / Doxey, Scott Doxey
  13. Modules / / utils.js export default function logMessage (message =

    '') { console.log(message); } / / main.js import {logMessage} from './utils.js'; logMessage('Hello, friend.');
  14. Object.assign const DEFAULT_SETTINGS = { sounds: 'beep', type: 'notice', color:

    'blue' } const logMessage = (content, options) => { const settings = Object.assign({}, DEFAULT_SETTINGS, options); console.log(settings); } logMessage('OMG LOOK OUT!', {type: 'error', color: 'red'}); / / Object {sounds: "beep", type: "error", color: "red"}
  15. Object.assign const DEFAULT_SETTINGS = { sounds: 'beep', type: 'notice', color:

    'blue' } const logMessage = (content, options) => { const settings = Object.assign({}, DEFAULT_SETTINGS, options); console.log(settings); } logMessage('OMG LOOK OUT!', {type: 'error', color: 'red'}); / / Object {sounds: "beep", type: "error", color: "red"}
  16. First install Babel via NPM. Then create a .babelrc file

    with references to the plugins you want to use. Then install the plugins via NPM. Repeat the same process if you need polyfills. Then either setup an NPM run script or use a build took like Gulp or Grunt with the appropriate plugins for Babel. If you want Babel to rebuild whenever you make a change you can either use a standalone node script to watch for changes or use a watch plugin for either Gulp or Grunt.
  17. Spire of Babel Features • Converts ES2015 to ES5 •

    Converts React (JSX) • Bundles code with Browserify • Lints code with ESLint • Generates Source Maps • Watch for changes and rebuild