gefvert.org

ES2015 for Dummies

2020-09-24

New class syntax:

class CoolStuff extends ParentStuff {
    constructor(name) {
        this.name = name;
        super(name);
    }
    
    static methodName() {
        return 'I am static!';
    }
    
    printName() {
        return 'I am dynamic!';
    }
    
    get area() {
        return this.height * this.weight;
    }
    
    set area() {
        this.area = value;
    }
}

Modules can be imported with (although webpack may be preferable):

<script type="module" src="./scripts/index.js"> </script>

Module import/export:

import { name1, name2, printMsg, fubarMethod as notNiceMethod } from './module1.js';
import * as module1Stuff from './module1.js';

export function printMsg(msg) { ...  };

export default ...

Keep in mind imports are constants.

Async declaration:

async function doSomething() {
    try {
        await doSomething("foo");
        await doSomething("bar");
    }
    catch (e) {
        console.error(e.message);
    }
}