Swizec Teller - a geek with a hatswizec.com

    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.


    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.


    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 :)

    Enhanced by Zemanta

    Did you enjoy this article?

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

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

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

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    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 bySwizecwith ❤️