past, present, future
what is it?
for scripting languages
first came out in 1997
although commonly known as synonymous with Javascript
but let's just skip the boring parts
first widely recognized version
came out in 2009
small step for man, giant leap for mankind
"use strict";
delete Object.prototype;
// This will cause an error
JSON.stringify()
JSON.parse()
of JS world
a.k.a. ECMAScript 2015
is a breakthrough adding a plethora of new features
var counter = 1;
for (var i = 0; i < 100; i++) {
counter++;
}
console.log(i)
const PI = 3.14
PI = 3 // This will cause an error
// Classes in ES5
var Shape = function (id, x, y) {
this.id = id
this.move(x, y)
}
Shape.prototype.move = function (x, y) {
this.x = x
this.y = y
}
// Classes in ES6
class Shape {
constructor (id, x, y) {
this.id = id
this.move(x, y)
}
move (x, y) {
this.x = x
this.y = y
}
}
// util.js
export function sum(a, b) {
return a + b
}
// main.js
import { sum } from './utils.js'
const result = sum(1, 2)
const numbers = [ 1, 2, 3 ]
const sum = (a, b) => return a + 1
numbers.map(number => sum(number, 10))
// expected output: Array [ 11, 12, 13 ]
a more structured way of handling callbacks
// ES5 - Callback hell
handleRequest(request, function(response) {
processResponse(response, function(result) {
writeToDatabase(result, function(successful) {
console.log(successful)
})
})
})
// ES6 - Promises
handleRequest(request)
.then(response => processResponse(response))
.then(result => writeToDatabase(result)
.then(successful => {
console.log(successful)
})
a.k.a. ECMAScript 2016, 2017
exponential operator and async/awaits
const square = x => x ** 2;
const cube = x => x ** 3;
async function handleRequest(request) {
try {
const result = await processResponse(response)
const successful = await writeToDatabase(result)
console.log(successful)
} catch (e) {
console.error(e)
}
}
what future holds
find me on github.com/koraytaylan