• Cosimo Orban

ES6 features to start with

Arrows =>

Arrows are a shorthand for function.


Old way:

users.forEach(function(user) {
    // do something with the user
});

New way:

users.forEach(user => {
    // do something with the user
})

Important tip: Arrow functions share this as the code surrounding them, unlike functions.


Classes

Classes are just sugar around OO prototype system.


Old way:

var Person = function (name) {
    this.name = name;
};
Person.prototype.getName = function () {
    return this.name;
}
var john = new Person('John Smith');
console.log(‘My name is‘, john.getName());

New way:

class Person {
    constructor(name) {
        this.name = name
    }
    getName() {
        return this.name
    }
}
var john = new Person('John Smith')
console.log(‘My name is, john.getName())

Template Strings

Old way:

var textConcatenation = 'Timestamp: ' + Date.now();

New way:

var templateStrings = `Timestamp: ${Date.now()}`

Modules

log.js

export const error = (message) => { console.error(message) }
export const warning = (message) => { console.warning(message) }

app.js

export * as Log from 'log'

# The message will be logged as error. This is a bad error message. More on error messages here: http://layer0.authentise.com/your-error-messages-are-bad-and-you-should-feel-bad.html
Log.warning('The system is overheating')
Log.error('Segmentation fault')

Default

Functions can now have arguments with default value

function sum(x, y=10) {
    return x+y
}

sum(1) // 11

Rest + Spread

Skip passing arguments one by one

function sum(...numbers) {
    return numbers.reduce((a, b) => a + b, 0)
}
var numbers = [1, 2, 3, 4]
sum(numbers) // 10

Once you've added the above new features into your code, you could continue to explore the many more features that 6th edition has to offer: http://www.ecma-international.org/ecma-262/6.0/index.html

0 views

Follow us at:

  • LinkedIn
  • Twitter
  • YouTube
  • Facebook

MES

MES for OEMs

MES for Service Bureaus

Extension Modules

About

Company Overview

Jobs

Support

Resources

Machine Integrations

Software Integrations

Contact Us

Philadelphia, PA, USA

Member of

Privacy Policy

Terms of Use

AUTHENTISE © COPYRIGHT 2020. ALL RIGHTS RESERVED.