=============================== Pythonic JavaScript with ES2015 =============================== We've seen a lot so far on the frontend toolchain, Pythonic development with modules, TDD, etc. But what about basic language features? JavaScript is notoriously more janky than Python. But what if you could code in a more Pythonic JavaScript, while still supporting most browsers? In this section we introduce the concept of *transpiling* pleasant JavaScript using `Babel `_. Overview ======== - Explain the concept of transpiling - Introduce Babel as the most popular transpiler - Show Pythonic idioms in latest JavaScript Background ========== JavaScript code is ugly and missing many features we take for granted in Python. However, it is a platform that is available *everywhere*. Even though it is unpleasant, people still target it for web development. In the past few years, a quiet revolution has taken place. What if we could treat the browser as a deployment target, and write our code in something nicer which "transpiles" into executable JavaScript? Python has a slightly similar concept: it reads pretty ``.py`` files and byte-compiles them into ``.pyc`` files. For the JavaScript compilers, the output is a flavor of JavaScript that is supported by many browsers, but using crazy techniques to cover various holes. This lets you write in a more pleasant input, transpile into "ES5" (an ECMAScript version supported by many browsers), and gain the productivity win you wanted. This revolution has freed the ECMAScript (aka JavaScript) standards committee to dramatically speed up the improvements to the language. It matters less whether browsers keep up, as we're just going to transpile these syntax improvements into compliant deployment output. Frontend developers now talk about "ES2015", the set of standards approved by the committee in the year 2015, as a group of improvements they can adopt. ES2016 and beyond are already in multiple stages of adoption. Fortunately PyCharm (and particularly WebStorm) have done a *fantastic* job of keeping up with this advanced pace of development. .. note:: These improvements were originally version-numbered, so you will frequently see "ES6". However, the committee adopted time-based releases, so the jargon is now "ES2015" etc. Starting Point ============== Let's use, as a starting point, a combination of results from earlier sections: - ``package.json`` for dependencies and automation - ``.eslintrc`` for controlling our linter, and thus PyCharm - Webpack for bundling our app file and our lib file We are going to do a variation on the ``incrementer`` pattern. In this case, when you click on a button, it adds a random number to a ``
  • `` list in the browser. The Incrementer =============== Let's jump into the results. We start with an ``index.html`` file: .. literalinclude:: index.html :language: html :caption: Pythonic JavaScript index.html :emphasize-lines: 9, 11, 12 This HTML file has a few responsibilities: - Provide a button which, when clicked, will add something to the ``