Slide 1

Slide 1 text

ES2015 Features And Your Rails App / Josiah Mory @kickinbahk

Slide 2

Slide 2 text

ES2015 - Released June 2016

Slide 3

Slide 3 text

Added New Syntax For "Complex" Applications

Slide 4

Slide 4 text

github.com/DrkSephy/es6-cheatsheet/

Slide 5

Slide 5 text

New Features Var vs Let/Const Replacing IIFEs with Blocks Arrow Functions Strings Destructuring Modules Parameters

Slide 6

Slide 6 text

Classes Symbols Maps WeakMaps New Features cont. Promises Generators

Slide 7

Slide 7 text

Var vs Let/Const Replacing IIFEs with Blocks Arrow Functions Parameters Strings New Features cont. Promises Generators

Slide 8

Slide 8 text

Var vs. Let & Const

Slide 9

Slide 9 text

Var is Scoped to the Function

Slide 10

Slide 10 text

f u n c t i o n f ( ) { f o r ( v a r i = 2 ; i < 1 0 ; i + = 1 ) { c o n s o l e . l o g ( " i = " + i ) ; } c o n s o l e . l o g ( i ) ; } f ( ) ;

Slide 11

Slide 11 text

Let and Const are Scoped to the Block

Slide 12

Slide 12 text

Const is a Constant Reference to a Value

Slide 13

Slide 13 text

Immutable when Referencing a Primitive (String, Num, Bool)

Slide 14

Slide 14 text

Not Immutable Referencing an Object (Arrays and Objects)

Slide 15

Slide 15 text

Less Strict Immutability

Slide 16

Slide 16 text

Let and Const f u n c t i o n m y F u n c ( ) { { l e t x ; { / / o k a y , b l o c k s c o p e d n a m e c o n s t x = " s n e a k y " ; / / e r r o r , c o n s t x = " f o o " ; } / / o k a y , d e c l a r e d w i t h ` l e t ` x = " b a r " ; / / e r r o r , a l r e a d y d e c l a r e d i n b l o c k l e t x = " i n n e r " ; } }

Slide 17

Slide 17 text

Replacing IIFEs with Blocks

Slide 18

Slide 18 text

Immediately Invoked Function Expression

Slide 19

Slide 19 text

Allows for scoping (To not pollute the global space)

Slide 20

Slide 20 text

Es5 IIFE: ( f u n c t i o n ( ) { v a r f o o d = ' M e o w M i x ' ; } ( ) ) ; c o n s o l e . l o g ( f o o d ) ; / / R e f e r e n c e E r r o r ES6 Blocks: { l e t f o o d = ' M e o w M i x ' ; } c o n s o l e . l o g ( f o o d ) ; / / R e f e r e n c e E r r o r

Slide 21

Slide 21 text

Arrow Functions

Slide 22

Slide 22 text

From Coffeescript (Fat Arrow)

Slide 23

Slide 23 text

Shorter Code... f u n c t i o n f o o ( x , y ) { r e t u r n x + y ; } / / v e r s u s v a r f o o = ( x , y ) = > x + y ;

Slide 24

Slide 24 text

More importantly is impact of t h i s

Slide 25

Slide 25 text

t h i s bindings are dynamic so we use the predictability of lexical scope via the self variable. v a r c o n t r o l l e r = { m a k e R e q u e s t : f u n c t i o n ( . . ) { v a r s e l f = t h i s ; b t n . a d d E v e n t L i s t e n e r ( " c l i c k " , f u n c t i o n ( ) { / / . . s e l f . m a k e R e q u e s t ( . . ) ; } , f a l s e ) ; } } ;

Slide 26

Slide 26 text

Inside arrow functions, the t h i s binding is not dynamic, but is instead lexical v a r c o n t r o l l e r = { m a k e R e q u e s t : f u n c t i o n ( . . ) { b t n . a d d E v e n t L i s t e n e r ( " c l i c k " , ( ) = > { / / . . t h i s . m a k e R e q u e s t ( . . ) ; } , f a l s e ) ; } } ;

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Parameters

Slide 29

Slide 29 text

Default Parameters

Slide 30

Slide 30 text

Es5 f u n c t i o n a d d T w o N u m b e r s ( x , y ) { x = x | | 0 ; y = y | | 0 ; r e t u r n x + y ; } Es6 f u n c t i o n a d d T w o N u m b e r s ( x = 0 , y = 0 ) { r e t u r n x + y ; }

Slide 31

Slide 31 text

Rest Parameter (Indefinite Amount of Args)

Slide 32

Slide 32 text

Rest Operator (...)

Slide 33

Slide 33 text

Es5 f u n c t i o n l o g A r g u m e n t s ( ) { f o r ( v a r i = 0 ; i < a r g u m e n t s . l e n g t h ; i + + ) { c o n s o l e . l o g ( a r g u m e n t s [ i ] ) ; } } Es6 f u n c t i o n l o g A r g u m e n t s ( . . . a r g s ) { f o r ( v a r i = 0 ; i < a r g s . l e n g t h ; i + + ) { c o n s o l e . l o g ( a r g s [ i ] ) ; } }

Slide 34

Slide 34 text

for loop f u n c t i o n l o g A r g u m e n t s ( . . . a r g s ) { f o r ( v a r i = 0 ; i < a r g s . l e n g t h ; i + + ) { c o n s o l e . l o g ( a r g s [ i ] ) ; } } for...of (Es6) f u n c t i o n l o g A r g u m e n t s ( . . . a r g s ) { f o r ( l e t a r g o f a r g s ) { c o n s o l e . l o g ( a r g ) ; } }

Slide 35

Slide 35 text

Strings

Slide 36

Slide 36 text

Adds New Methods to the Library

Slide 37

Slide 37 text

.includes()

Slide 38

Slide 38 text

Es5 v a r s t r i n g = ' f o o d ' ; v a r s u b s t r i n g = ' f o o ' ; c o n s o l e . l o g ( s t r i n g . i n d e x O f ( s u b s t r i n g ) > - 1 ) ; Es6 c o n s t s t r i n g = ' f o o d ' ; c o n s t s u b s t r i n g = ' f o o ' ; c o n s o l e . l o g ( s t r i n g . i n c l u d e s ( s u b s t r i n g ) ) ; / / t r u e

Slide 39

Slide 39 text

.repeat()

Slide 40

Slide 40 text

Es5 f u n c t i o n r e p e a t ( s t r i n g , c o u n t ) { v a r s t r i n g s = [ ] ; w h i l e ( s t r i n g s . l e n g t h < c o u n t ) { s t r i n g s . p u s h ( s t r i n g ) ; } r e t u r n s t r i n g s . j o i n ( ' ' ) ; } Es6 / / S t r i n g . r e p e a t ( n u m b e r O f R e p e t i t i o n s ) ' m e o w ' . r e p e a t ( 3 ) ; / / ' m e o w m e o w m e o w '

Slide 41

Slide 41 text

Template Literals

Slide 42

Slide 42 text

Allows special characters w/o Escaping

Slide 43

Slide 43 text

Es5 v a r t e x t = " T h i s s t r i n g c o n t a i n s \ " d o u b l e q u o t e s \ " w h i c h a r e e s c a p e d . " ; Es6 v a r t e x t = ` T h i s s t r i n g c o n t a i n s " d o u b l e q u o t e s " w h i c h a r e e s c a p e d . ` ;

Slide 44

Slide 44 text

String Interpolation

Slide 45

Slide 45 text

Es5 v a r n a m e = ' T i g e r ' ; v a r a g e = 1 3 ; c o n s o l e . l o g ( ' M y c a t i s n a m e d ' + n a m e + ' a n d i s ' + a g e + ' y e a r s o l d . ' ) ; Es6 c o n s t n a m e = ' T i g e r ' ; c o n s t a g e = 1 3 ; c o n s o l e . l o g ( ` M y c a t i s n a m e d $ { n a m e } a n d i s $ { a g e } y e a r s o l d . ` ) ;

Slide 46

Slide 46 text

New Line Preservation

Slide 47

Slide 47 text

Es5 v a r t e x t = ( ' c a t \ n ' + ' d o g \ n ' + ' n i c k e l o d e o n ' ) ; Es6 l e t t e x t = ( ` c a t d o g n i c k e l o d e o n ` ) ;

Slide 48

Slide 48 text

Promises

Slide 49

Slide 49 text

A Promise object represents a value that may not be available yet.

Slide 50

Slide 50 text

Allow replacing Callbacks with Promises

Slide 51

Slide 51 text

Makes for more readable Code

Slide 52

Slide 52 text

Callbacks f u n c 1 ( f u n c t i o n ( v a l u e 1 ) { f u n c 2 ( v a l u e 1 , f u n c t i o n ( v a l u e 2 ) { f u n c 3 ( v a l u e 2 , f u n c t i o n ( v a l u e 3 ) { f u n c 4 ( v a l u e 3 , f u n c t i o n ( v a l u e 4 ) { f u n c 5 ( v a l u e 4 , f u n c t i o n ( v a l u e 5 ) { / / D o s o m e t h i n g w i t h v a l u e 5 } ) ; } ) ; } ) ; } ) ; } ) ;

Slide 53

Slide 53 text

Promises f u n c 1 ( v a l u e 1 ) . t h e n ( f u n c 2 ) . t h e n ( f u n c 3 ) . t h e n ( f u n c 4 ) . t h e n ( f u n c 5 , v a l u e 5 = > { r e s o l v e ( 5 + 1 ) ; / / D o s o m e t h i n g w i t h v a l u e 5 r e j e c t ( ) ; } ) . c a t c h ( e r r o r ) ;

Slide 54

Slide 54 text

Generators

Slide 55

Slide 55 text

New Type of Function

Slide 56

Slide 56 text

Standard Function is "Run to Completion"

Slide 57

Slide 57 text

With ES6 generators, we have a different kind of function

Slide 58

Slide 58 text

These new functions may be paused, and resumed later

Slide 59

Slide 59 text

This allows other code to run during these paused periods

Slide 60

Slide 60 text

It can be paused by using the y i e l d keyword inside the Generator

Slide 61

Slide 61 text

Nothing from the outside of a Generator can stop it

Slide 62

Slide 62 text

Once paused, only something outside can restart it

Slide 63

Slide 63 text

You would do this using the r e t u r n statement.

Slide 64

Slide 64 text

This enables 2-way message passing, to and from the Generator

Slide 65

Slide 65 text

(Two different naming conventions) f u n c t i o n * f o o ( ) { / / . . } or f u n c t i o n * f o o ( ) { / / . . }

Slide 66

Slide 66 text

It is just a normal function, with different keywords

Slide 67

Slide 67 text

y i e l d is referred to as a: y i e l d e x p r e s s i o n

Slide 68

Slide 68 text

What we send back in is the result of the y i e l d e x p r e s s i o n

Slide 69

Slide 69 text

f u n c t i o n * f o o ( ) { y i e l d 1 ; y i e l d 2 ; y i e l d 3 ; y i e l d 4 ; y i e l d 5 ; }

Slide 70

Slide 70 text

To step through values, we need an iterator

Slide 71

Slide 71 text

v a r i t = f o o ( ) ;

Slide 72

Slide 72 text

We have the .next()

Slide 73

Slide 73 text

v a r i t = f o o ( ) ; c o n s o l e . l o g ( i t . n e x t ( ) ) ; / / { v a l u e : 1 , d o n e : f a l s e }

Slide 74

Slide 74 text

If we keep interating c o n s o l e . l o g ( i t . n e x t ( ) ) ; / / { v a l u e : 2 , d o n e : f a l s e } c o n s o l e . l o g ( i t . n e x t ( ) ) ; / / { v a l u e : 3 , d o n e : f a l s e } c o n s o l e . l o g ( i t . n e x t ( ) ) ; / / { v a l u e : 4 , d o n e : f a l s e } c o n s o l e . l o g ( i t . n e x t ( ) ) ; / / { v a l u e : 5 , d o n e : f a l s e } c o n s o l e . l o g ( i t . n e x t ( ) ) ; / / { v a l u e : u n d e f i n e d , d o n e : t r u e }

Slide 75

Slide 75 text

Let's look at a slightly more complex example

Slide 76

Slide 76 text

f u n c t i o n * f o o ( x ) { v a r y = 2 * ( y i e l d ( x + 1 ) ) ; v a r z = y i e l d ( y / 3 ) ; r e t u r n ( x + y + z ) ; } v a r i t = f o o ( 5 ) ; / / n o t e : n o t s e n d i n g a n y t h i n g i n t o n e x t ( ) h e r e c o n s o l e . l o g ( i t . n e x t ( ) ) ; / / { v a l u e : 6 , d o n e : f a l s e } c o n s o l e . l o g ( i t . n e x t ( 1 2 ) ) ; / / { v a l u e : 8 , d o n e : f a l s e } c o n s o l e . l o g ( i t . n e x t ( 1 3 ) ) ; / / { v a l u e : 4 2 , d o n e : t r u e }

Slide 77

Slide 77 text

The Basics Of ES6 Generators https://davidwalsh.name/es6-generators

Slide 78

Slide 78 text

Adding ES2015 to Your Rails App

Slide 79

Slide 79 text

2 gems are Necessary

Slide 80

Slide 80 text

# G e m f i l e g e m " s p r o c k e t s " g e m " s p r o c k e t s - e s 6 "

Slide 81

Slide 81 text

Add to top of a p p l i c a t i o n . j s r e q u i r e ' s p r o c k e t s / e s 6 '

Slide 82

Slide 82 text

Install Presets n p m i n s t a l l b a b e l - p r e s e t - e s 2 0 1 5 - - s a v e - d e v

Slide 83

Slide 83 text

Create . b a b e l r c config and enable Plugin(s) e c h o ' { " p r e s e t s " : [ " e s 2 0 1 5 " ] } ' > . b a b e l r c

Slide 84

Slide 84 text

Es6 functionality is added to any . e s 6 file

Slide 85

Slide 85 text

Instructions From Babel Website ( ) https://babeljs.io/docs/setup/#rails

Slide 86

Slide 86 text

ES2015 Spec