On Github thedamon / es6talk
E to the C to the M to the A to the...
Show of Hands: -who uses javascript in their job? knows about ES6? tried it? -at work, -at home on personal projects, at work on personal projects -have you used it in production?//install [4] * [4] // 16 [] * [] // 0 [] * {} // NaN [4, 4] * [4, 4] // NaN ({} * {}) // NaN
An S? Seriously?
//tinyPiglet.js export const numEars = 2; export default rubTummy function(){ return "squeak!"; } privateMethod = function(){ return "well I never!"; } //or export {numEars, rubTummy}
import {rubTummy, numberOfEarsOnAPiglet as numEars} from "tinyPiglet.js"; rubTummy(); //"squeak!" console.log(numEars); //2 import * as piglets from "tinyPiglet.js"; //everything piglets.rubTummy() //"squeak"! import "tinyPiglet.js"; //side-effects only import tinyPiglet from "tinyPiglet"; //default import
Remember how so many people complain that javascript isn't a real programming language because it doesn't have classes? Even if you don't use them, it will make you feel better knowing that they're there.
But it's really just prototypal inheritance
import { cuteMeatAnimal } from 'cuteMeatAnimal'; class tinyPiglet extends cuteMeatAnimal { export const numEars = 2; constructor(){ console.log('Stumble in and roll about on the floor.'); } export default static rubTummy function(){ //Static: tinyPiglet.rubTummy return "squeak!"; } privateMethod = function(){ //instance: new TinyPiglet().privateMethod() return "well I never!"; } }
var cat, toys = [], scritchesPerMinute; let myCatsMood = 'cheerful'; const myCatsDisdainForMe = 'epic'; myCatsDisdainFormMe = 'gone!!' // ERROR!
You maay have seen this before in another language...
function feedCat( food = 'catfood', amount = function(){Math.random()}, foodBrand = food + 'Co' ){ if (food === 'human') callPolice(); } feedCat() //will have defaults!In the spirit of javascript, these can be any value... like a function!
//ES5ish function(number) { return number % 2; } //ES6!!! number => number % 2 (parameters) => {} // braces will *remove* implicit return
//ES5ish var example = 'This' + fetchCompliment() + 'string can go \ to another line and keep going' //ES6!!! let example = `This ${fetchCompliment()} string can go to another line and keep going`;
Spread is like a cross section of an array.
let partsToPet = ['back', 'tummy', 'tail'] petTheseParts(...partsToPet); //petTheseParts('back', 'tummy,', 'tail') //Trick! quickly convert an object or collection to an array [...document.querySelectorAll('')].forEach{...} //or array building: let foo = [2, 3, 4]; console.log(1, ...foo, 5) // '1 2 3 4 5'
Rest is not sleep 😟. PHP calls this the 'splat' operator💦.
//a flexible number of parameters function petTheseParts(...parts){ console.log(parts) //['back', 'tummy', 'tail']; }Kind of like destructuring.
Pull things out of an array
//response.kittens.thisKitten = {name: 'Smuggler'} let {name, hairColour, favouriteSpot = 'myChair'} = response.kittens.thisKitten; name //"Smuggler" vs console.log(response.kittens.thisKitten.name) hairColour //undefined favouriteSpot //myChair
You can rename what you extract but it's backwards 🔄
let {name: nameOfTheKittenIWillPet} = response.kittens.thisKitten; name //undefined theNameOfTheKittenIWillPet //Smuggler //function parameter defaults when you're CALLING them? shooCat(favouriteSpot = 'keyboard'); //or this ridiculousness: var { x: { y: { z: w } } } = o1;
This one is pretty straightforward, but awesome!
function updateKitten(newKitten){ Object.assign({}, defaultKitten, currentKitten, newKitten); } let currentKitten = {favouriteToy: 'packing peanuts'} inspectKitten(currentKitten); updateKitten(currentKitten, {favoriteToy: 'my soft, mushy fingers'});
{ [surprisePropertyName()]: 'uuuuhh... 7?', ['theHolySwordOf' + myName]: swordStats }
Object.is({ name }, {name: name}); //trueCPN: Make sure these are predictible.
//install $ npm install --save-dev babel-loader babel-core
//project config: module: { loaders: [ { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"} ] } //or per file: var Person = require("babel!./Person.js").default;
$ npm install --save-dev grunt-babel
grunt.initConfig({ "babel": { options: { sourceMap: true }, dist: { files: { "dist/app.js": "src/app.js" } } } }); grunt.registerTask("default", ["babel"]);
And everything in between
And yes, this does all assume you're using NPM for package management.