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

CoffeeScript Einführung

ipf
November 27, 2012

CoffeeScript Einführung

Lightning Talk bei der SUB Entwicklerrunde

ipf

November 27, 2012
Tweet

More Decks by ipf

Other Decks in Programming

Transcript

  1. $ WHOAMI Ingo Pfennigstorf Medieninformatiker > 30 Webentwickler SUB Team

    WWW, Portale TYPO3 Entwicklung Frontendgeraffel (JavaScript, ...)
  2. INSTALLATION node.js / npm ( s u d o )

    n p m i n s t a l l - g c o f f e e - s c r i p t
  3. VARIABELN wird kompiliert zu n u m b e r

    O f T h e B e a s t = 6 6 6 i s D e v i l = t r u e v a r i s D e v i l , n u m b e r O f T h e B e a s t ; n u m b e r O f T h e B e a s t = 6 6 6 ; i s D e v i l = t r u e ;
  4. CONDITIONS wird kompiliert zu n u m b e r

    O f T h e B e a s t = 6 6 6 i s D e v i l s N e i g h b o u r = t r u e n u m b e r O f T h e B e a s t = 6 6 7 i f i s D e v i l s N e i g h b o u r v a r i s D e v i l s N e i g h b o u r , n u m b e r O f T h e B e a s t ; n u m b e r O f T h e B e a s t = 6 6 6 ; i s D e v i l s N e i g h b o u r = t r u e ; i f ( i s D e v i l s N e i g h b o u r ) { n u m b e r O f T h e B e a s t = 6 6 7 ; }
  5. CONDITIONS wird kompiliert zu e l v i s =

    ' a l i v e ' a l e r t " I k n e w i t ! " i f e l v i s ? v a r e l v i s ; e l v i s = ' a l i v e ' ; i f ( e l v i s ! = n u l l ) { a l e r t ( " I k n e w i t ! " ) ; }
  6. BEISPIELE - FUNKTIONEN wird kompiliert zu s q u a

    r e = ( x ) - > x * x v a r s q u a r e ; s q u a r e = f u n c t i o n ( x ) { r e t u r n x * x ; } ;
  7. OBJEKTE wird kompiliert zu m a t h = r

    o o t : M a t h . s q r t s q u a r e : s q u a r e c u b e : ( x ) - > x * s q u a r e x m a t h = { r o o t : M a t h . s q r t , s q u a r e : s q u a r e , c u b e : f u n c t i o n ( x ) { r e t u r n x * s q u a r e ( x ) ; } } ;
  8. FUNKTIONEN wird kompiliert zu l i s t = [

    1 , 2 , 3 , 4 , 5 ] c u b e s = ( m a t h . c u b e n u m f o r n u m i n l i s t ) v a r c u b e s , l i s t , n u m ; l i s t = [ 1 , 2 , 3 , 4 , 5 ] ; c u b e s = ( f u n c t i o n ( ) { v a r _ i , _ l e n , _ r e s u l t s ; _ r e s u l t s = [ ] ; f o r ( _ i = 0 , _ l e n = l i s t . l e n g t h ; _ i < _ l e n ; _ i + + ) { n u m = l i s t [ _ i ] ; _ r e s u l t s . p u s h ( m a t h . c u b e ( n u m ) ) ; } r e t u r n _ r e s u l t s ; } ) ( ) ;
  9. SPLATS wird kompiliert zu Ausgabe: 4 [ 5 , 2

    2 , 3 3 , 4 2 , 5 5 , 4 ] r a c e = ( w i n n e r , r u n n e r s . . . ) - > c o n s o l e . l o g ( w i n n e r , r u n n e r s ) r a c e ( 4 , 5 , 2 2 , 3 3 , 4 2 , 5 5 , 4 ) v a r r a c e , _ _ s l i c e = [ ] . s l i c e ; r a c e = f u n c t i o n ( ) { v a r r u n n e r s , w i n n e r ; w i n n e r = a r g u m e n t s [ 0 ] , r u n n e r s = 2 < = a r g u m e n t s . l e n g t h ? _ _ s l i c e . c a l l ( a r g u m e n t s , 1 ) : [ ] ; r e t u r n c o n s o l e . l o g ( w i n n e r , r u n n e r s ) ; } ; r a c e ( 4 , 5 , 2 2 , 3 3 , 4 2 , 5 5 , 4 ) ;
  10. TRY COFFEESCRIPT! CoffeeScript is a little language that compiles into

    JavaScript. Underneath all those awkward braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one­to­one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice­versa). The compiled output is readable and pretty­printed, passes through JavaScript Lint without warnings, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript. Latest Version: 1.4.0 Overview Overview CoffeeScript on the left, compiled JavaScript output on the right. # A s s i g n m e n t : n u m b e r = 4 2 o p p o s i t e = t r u e v a r c u b e s , l i s t , m a t h , n u m , n u m b e r , o p p o s i t e , r a c e , s _ _ s l i c e = [ ] . s l i c e ; n u m b e r = 4 2 ; TABLE OF CONTENTS TRY COFFEESCRIPT Run Link Link ANNOTATED SOURCE
  11. U s a g e : c o f f

    e e [ o p t i o n s ] p a t h / t o / s c r i p t . c o f f e e - - [ a r g s ] I f c a l l e d w i t h o u t o p t i o n s , ` c o f f e e ` w i l l r u n y o u r s c r i p t . < / p r e > - b , - - b a r e c o m p i l e w i t h o u t a t o p - l e v e l f u n c t i o n w r a p p e r - c , - - c o m p i l e c o m p i l e t o J a v a S c r i p t a n d s a v e a s . j s f i l e s - e , - - e v a l p a s s a s t r i n g f r o m t h e c o m m a n d l i n e a s i n p u t - h , - - h e l p d i s p l a y t h i s h e l p m e s s a g e - i , - - i n t e r a c t i v e r u n a n i n t e r a c t i v e C o f f e e S c r i p t R E P L - j , - - j o i n c o n c a t e n a t e t h e s o u r c e C o f f e e S c r i p t b e f o r e c o m p i l i n g - l , - - l i n t p i p e t h e c o m p i l e d J a v a S c r i p t t h r o u g h J a v a S c r i p t L i n t - n , - - n o d e s p r i n t o u t t h e p a r s e t r e e t h a t t h e p a r s e r p r o d u c e s - - n o d e j s p a s s o p t i o n s d i r e c t l y t o t h e " n o d e " b i n a r y - o , - - o u t p u t s e t t h e o u t p u t d i r e c t o r y f o r c o m p i l e d J a v a S c r i p t - p , - - p r i n t p r i n t o u t t h e c o m p i l e d J a v a S c r i p t - r , - - r e q u i r e r e q u i r e a l i b r a r y b e f o r e e x e c u t i n g y o u r s c r i p t - s , - - s t d i o l i s t e n f o r a n d c o m p i l e s c r i p t s o v e r s t d i o - t , - - t o k e n s p r i n t o u t t h e t o k e n s t h a t t h e l e x e r / r e w r i t e r p r o d u c e - v , - - v e r s i o n d i s p l a y t h e v e r s i o n n u m b e r - w , - - w a t c h w a t c h s c r i p t s f o r c h a n g e s a n d r e r u n c o m m a n d s
  12. BEISPIELE c o f f e e - - w

    a t c h - - c o m p i l e - o . . / . . / P u b l i c / J a v a S c r i p t * . c o f f e e c o f f e e - - j o i n S u b . j s - - c o m p i l e . . / . . / P u b l i c / J a v a S c r i p t / * . c o f f e e
  13. FURTHER READING coffeescript.org CoffeeScript Cookbook The Little Book on CoffeeScript

    Dropbox dives into CoffeeScript 10 CoffeeScript One Liners to Impress Your Friends