Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    The exciting future Javascript

    ECMAscript, 6th edition might bring us fat arrow notation - Douglas Crockford on fat arrows

    function (x) {
        return x * x;
    }
    
    // becomes
    
    (x) => x * x
    

    Douglas' post concerns itself mostly with the intricacies of how this is bound to the function objectand what the fat arrow notation might bring, what I see is something completely different.

    vim
    vim

    Proper lambdas!

    That's right, functions that implicitly return with none of that big ugly function keyword! Hooray.

    A bit tricky when you need a named function, but so far this is just an unofficial proposal, I'm sure they'll figure something out by the time ECMA6 comes anywhere near being a standard (not in 2012).

    As I said on hacker news Javascript is a beautiful language, trapped in bad syntax, peppered with poor semantic choices. What is left of a language once you take syntax and semantics out of the picture? Quite a lot.

    What else is new

    Spurred on by excitement I decided to poke around the wiki for the ongoing specification work of Ecma.

    Some of the "tentatively approved" proposals include:

    1. Array comprehensions and generators and iterators - a natural notation for constructing lists from lists, used a lot in python and haskell.

      // mapping
      [ square(x) for (x of [1,2,3,4,5]) ]
      // filtering
      [ x for (x of a) if (x.color === ‘blue’) ]
      

      This naturally extends into generators, where you can have an object generating values according to a pattern - possibly until infinity. (the example given in the proposal are fibonacci numbers, obviously)

    Iterators are a similar beast - you get to define how an object should be iterated over, which gives us nice abstractions for all sorts of things.

    1. **Classes **- although using prototypes, functions and instances is enough to do everything classes can do, it isn't very expressive and too much deep knowledge is required to understand the code.

      A class defines four objects and their properties: a constructor function, a prototype, a new instance, and a private record bound to the new instance. The body of a class is a collection of member definitions.

    2. Block scoped bindings - Javascript used to only be lexically scoped, otherwise known to new programmers as "Aaaaaah everything is global what the hell!?". With the addition of let, const and block functions there will now also be block scope where you can define things to only exist inside two .

    3. Modules!

    What has traditionally been solved using require.js will now work similar to how python does it. You get to define modules and then import them where they are needed. Possibly the most earth shattering addition since dependency management in Javascript has devolved into an extreme sport lately.

    // module
    module math {
        export function sum(x, y) {
            return x + y;
        }
        export var pi = 3.141593;
    }
    
    // client
    // we can import in script code, not just inside a module
    import {sum, pi} from math;
    
    alert("2π = " + sum(pi, pi));
    
    1. Proxies - according to the proposal a Proxy is an object that takes two objects - a target and a handler. Where, if I understand correctly, you call functions on the proxy (trigger them), which execute as traps on the handler object, using the target.

    This looks like a new way of handling events.Similar to what modern MVC frameworks are doing where you have a "view" that takes care of safely executing functions when events are triggered.

    When?

    A lot more interesting stuff can be found in the strawman section where completely far out ideas live, but the tentatively approved ideas already raise my hopes far too much without any assurance I'll ever get any of this. So much could change before any of this is solidifies into a standard.

    And even when it does become a standard ... how long before any of this reaches wide browser support? Mozilla's Javascript already includes a lot of this stuff - a lot of the proposals come from there actually - anyone else? Not really.

    At least there is some comfort in the fact all of this will quickly reach node.js :)

    Published on April 2nd, 2012 in Constructor (object-oriented programming), Definition, Douglas Crockford, ECMAScript, Function object, functions, JavaScript, Scope (computer science), Uncategorized

    Did you enjoy this article?

    Continue reading about The exciting future Javascript

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

    Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️