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

Extending JavaScript the easy way

Extending JavaScript the easy way

Sergi shows an easy way to extend the syntax of JavaScript without having to create a new language. Using sweet.js macros created by Mozilla's Tim Disney, we can extend our favorite language without making big sacrifices, we can add a few small features to our favorite language or we can create entire DSLs that help with the problem at hand, all in an easy way.


Sergi Mansilla

November 01, 2012

More Decks by Sergi Mansilla

Other Decks in Technology


  1. Extending JavaScript the easy way Sergi Mansilla, November 2012

  2. @sergimansilla

  3. JavaScript is popular

  4. It is ASM for the web

  5. CoffeeScript LispyScript JWacs FlapJax Elm Coco FBJS JS11 Roy Streamline

    JS TypeScript Objective J JS2 Jack Source: http://altjs.org
  6. Be JavaScript, my friend

  7. ...but it is far from perfect

  8. None
  9. Macros ultimate flexibility (ES6 won't have them, btw)

  10. Macros provide a way to extend syntax Syntax Syntax Macro

  11. Users can extend the language

  12. It helps evolving the language faster

  13. sweet.js Macros for JavaScript

  14. sweet.js Modeled after Scheme macros

  15. Sweet.js Syntax Syntax Macro JS + Macros JS Sweet.js

  16. Redefine 'function'

  17. Redefine 'function'

  18. Destructuring

  19. Destructuring

  20. Destructuring

  21. Destructuring

  22. Destructuring

  23. Destructuring

  24. Destructuring

  25. ?

  26. ?

  27. Nice ranges!

  28. case-style assignment

  29. case-style assignment

  30. Use cases Try proposed syntax before it makes into the

    language Create Micro-DSLs for your scenario Use ideas from other languages Make the new <your-silly-name-here>Script Implement basic types You get the idea
  31. Start using it now > npm install sweet.js > sjs

    -o output.js test_macros.sjs > node output.js Node.js Browser https://github.com/iammerrick/require-sweet
  32. Questions?

  33. Thanks! @sergimansilla github.com/sergi sergimansilla.com

  34. http://sweetjs.org/ https://github.com/mozilla/sweet.js http://jlongster.com/why-sweet.js-matters http://scriptogr.am/micmath/post/sweet-macros-in- javascripthttps://news.ycombinator.com/item?id=4560691 http://youtu.be/_ahvzDzKdB0 References