Web-007 Javascript: © Luxoft Training. All Rights Reserved
Web-007 Javascript: © Luxoft Training. All Rights Reserved
ver. 1.0
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
WEB-007 JavaScript
Future of JavaScript
ver. 1.0
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
ECMAScript 6
• Arrows
• Classes
• Template strings
• Destructuring
• Modules
• Proxies
• Promises
• Tail calls
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Arrows
A function shorthand using the => syntax
this._friends.forEach( this._friends.forEach(
function (f) { f =>
return console.log( console.log(
that._name + this._name +
" knows " + f); " knows " + f));
});
} }
}; }
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Classes
move(dx, dy) {
this._x += dx;
this._y += dy;
}
}
class Circle extends Shape {
constructor(x, y, r) {
super(x, y);
this._r = r;
}
}
var c = new Circle(1, 1, 0.5);
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Enhanced object literals
var obj = {
// __proto__
__proto__: theProtoObj,
// Shorthand for ‘handler: handler’
handler,
// Methods
toString() {
// Super calls
return "d " + super.toString();
},
// Computed (dynamic) property names
[ 'prop_' + (() => 42)() ]: 42
};
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Template string
// Basic literal string creation
`In JavaScript '\n' is a line-feed.`
// Multiline strings
`In JavaScript this is
not legal.`
// String interpolation
var name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Destructuring
// list matching
var [a, , b] = [1,2,3];
// object matching
var { op: a, lhs: { op: b }, rhs: c }
= getASTNode()
// Fail-soft destructuring
var [a] = [];
a === undefined;
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Modules
// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
alert("2π = " + math.sum(math.pi, math.pi));
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Proxies
// Proxying a normal object
var target = {};
var handler = {
get: function (receiver, name) {
return `Hello, ${name}!`;
}
};
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Promises
function timeout(duration = 0) {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration);
})
}
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
Tail calls
function factorial(n, acc = 1) {
'use strict';
if (n <= 1) return acc;
return factorial(n - 1, n * acc);
}
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved
ECMAScript 6
• Arrows
• Classes
• Template strings
• Destructuring
• Modules
• Proxies
• Promises
• Tail calls
http://www.luxoft-training.ru/
© Luxoft Training. All rights reserved