+ All Categories
Home > Technology > Es6 everywhere

Es6 everywhere

Date post: 14-Apr-2017
Category:
Upload: adam-klein
View: 276 times
Download: 0 times
Share this document with a friend
39
ES6 Everywhere Adam Klein
Transcript
Page 1: Es6 everywhere

ES6 Everywhere

Adam Klein

Page 2: Es6 everywhere

The “future” is hereClasses & Inheritance Promises Modules Arrow functions Default parameters and more…

Page 3: Es6 everywhere

ES 2015

Page 4: Es6 everywhere

Adam Klein

- Developing for >18 years - Development, Consulting & Training

in Angular, React & Node - International clients

CTO @ 500Tech

Page 5: Es6 everywhere

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa

Page 6: Es6 everywhere

Browser Compatibility

Meaningless

Transpilers will always precede the slowest browser

http://kangax.github.io/compat-table/es6/

Page 7: Es6 everywhere

Babel JS transpiler Compatibility Speed Debugging Plugins JSX Output code

Page 8: Es6 everywhere

Babel JS $ npm i -g babel

playgrounds: codepen.io (choose babel preprocessor) babeljs.io/repl $ babel file.js // see transpiled $ babel-node file.js // run

Page 9: Es6 everywhere

ES6

Page 10: Es6 everywhere

Classesclass Point { constructor(x, y) { this.x = x; this.y = y; } move(x, y) { this.x += x; this.y += y; } }

Page 12: Es6 everywhere

modules

// tree.ctrl.js import BaseCtrl from ‘base';

class TreeCtrl extends BaseCtrl { }

// base.ctrl.js export default class BaseCtrl { }

Page 13: Es6 everywhere

Arrow functions and lexical ‘this’

var self = this; http.get(‘/people').then(function(people) { self.people = people; });

constructor() { http.get('/people').then((people) => { this.people = people; }); }

Page 14: Es6 everywhere

people.map(function(p) { return p.name; });

people.map(p => p.name);

Sugar!

Page 15: Es6 everywhere

Default parameters

http(url, method='GET', headers={})

http(url, method, headers) { method = method || 'GET'; headers = headers || {}; }

Page 16: Es6 everywhere

More cool features I bet you didn’t know

Page 17: Es6 everywhere

Rest & Spread// rest function format(str, ...args) { args.forEach(...); } // spread let arr1 = [1,2,3]; Math.max(...arr1);

// spread in array literal let arr2 = [4,5,6]; let arr3 = [...arr1, ...arr2];

Page 18: Es6 everywhere

“Named” parametersfunction connect( {host = ‘localhost', port = 9000, username, password} = {})

connect({port: 9002, username: ‘adam'}); connect();

Page 19: Es6 everywhere

ES7

function connect( {host = ‘localhost', port = 9000, username, password, ...opts} = {}) { }

Page 20: Es6 everywhere

string templates (String Interpolation)

let name = 'Adam Klein' template = `<div> <h1>Hello ${name}</h1> <a>Have a good day!</a> </div> `

Page 21: Es6 everywhere

ES6 in Angular

Page 22: Es6 everywhere

Using in Angularclass Auth { constructor($http) { this.$http = $http; } get(x) { return this.$http.get('...'); } } export Auth;

Page 23: Es6 everywhere

Registering with Angularimport { Auth } from './auth'; import angular from 'angular';

angular.module('Demo.services', []) .service('Auth', Auth);

Page 24: Es6 everywhere

Magic assignment

class HomeController { constructor($scope, $q, $timeout, Auth, Modal) { this.$scope = $scope; this.$q = $q; this.$timeout = $timeout; this.Auth = Auth; this.Modal = Modal; } }

Object.assign(this,{$scope, $q, $timeout, Auth, Modal});

Page 25: Es6 everywhere

Destructuring

{$scope, $q, $timeout, Auth, Modal} == { $scope: $scope, $q: $q, $timeout, $timeout, Auth: Auth, Modal: Modal

}

Page 26: Es6 everywhere

Webpacksimple config file

super fast dev server (in-memory transpilation)

import whatever (js, css, html, json)

possibility to pack chunks for lazy loading

Page 27: Es6 everywhere

‘Kick’ - Angular best practicesGreat way to see angular + webpack + ES6 in action

$ npm i -g kick (requires node >= 4.0)

$ kick n app

$ kick g service Auth

$ kick g state users

$ kick start

$ kick tdd

$ kick bundle

* disclaimer - WIP

www.angular-kick.com

Page 28: Es6 everywhere

Debuggingsource maps

debug in inspector normally

Page 29: Es6 everywhere

Tests using ES6webpack + karma

configured in kick

share mocks between dev & unit-tests & integration tests

Page 30: Es6 everywhere

Config files using ES6 (node 4.2.1)webpack config file

gulpfile

karma config file

etc.

Page 31: Es6 everywhere

Angular 2 https://github.com/500tech/angular2-webpack-es6

Page 32: Es6 everywhere

React https://github.com/500tech/react-webpack-es6

Page 33: Es6 everywhere

Component = classimport React, { Component } from 'react'; import { Row, Col } from 'react-bootstrap';

export class Root extends Component { render() { return ( <Row> <Col md={ 8 }> Office Radio { this.props.children } </Col> </Row> ); } }

Page 34: Es6 everywhere

Node

Page 35: Es6 everywhere

Node 4.2.1https://nodejs.org/en/docs/es6/ ClassesPromises Arrow functions let & const Template strings more…

Page 36: Es6 everywhere

Babel-nodeuse for development

$ babel-node server.js

$ babel-node-debug server.js

production

$ babel src —out-dir dist

Page 37: Es6 everywhere

Express hello world ES6https://github.com/500tech/nodejs-express-es6

Page 38: Es6 everywhere

Check the slides:

Follow us:http://blog.500tech.com

Keep in touch:

@500techil

Questions?

http://www.slideshare.net/500tech/es6-everywhere

[email protected]

Page 39: Es6 everywhere

THANK YOU


Recommended