A complement to Java
for easy online application development
Built on V8 JavaScript engine
Browsers war focuses on JavaScript engine speed
ES5 is mostly a compatibility version (ES4 was abandonned)
JSON
supportArray
and Object
methods"use strict"
to convert mistakes into errorsFunction.prototype.bind
this
valueFunctions...
Object
+ own executable codeObject
can benew
operatorJavaScript does not use classical
inheritance
JavaScript uses Prototype
inheritance
You should learn Prototype
inheritance
function Gentleman(){};
Gentleman.prototype.sayHello = function(){
console.log("Hello Sir");
}
var gentleman = new Gentleman();
gentleman.sayHello(); // 'Hello Sir'
Gentleman.prototype.sayHello = function(){
console.log("What's up dude ?");
}
gentleman.sayHello(); // What's up dude ?
this
value
function foo(){
console.log(this); // What is `this` ?
} // Looking at this, we can't say.
The value of this
in a function
only depends on its invocation mode*
not on its declaration
* There are 4 invocations modes
this
valuefoo(); //this === window
bar.foo(); //this === bar
new foo(); //this === [new built and returned object]
call
& apply
foo.call(baz); //this === baz
foo.apply(baz); //this === baz
Exceptions : Function.protoype.bind and Arrow function expressions
Some features already available
node --harmony
Extract data from arrays or objects using a syntax that mirrors
the construction of array and object literals
var a, b;
[a, b] = [1, 2];
// a === 1
// b === 2
[a, b] = [b, a];
// a === 2
// b === 1
[a, ,b] = ["v", "w", "x"];
// a === "v"
// b === "x"
Special functions that generate iterators
yield
provoke partial returns
Can be resume with next
method
function* greatGenerator(){
yield "Clermont'ech";
yield "is great";
for (var i = 0; i < 3; i++){
yield i*2;
}
}
var iterator = greatGenerator();
iterator.next(); // "Clermont'ech"
iterator.next(); // "is great"
iterator.next(); // 0
iterator.next(); // 2
Generators introduce a new expression syntax
//Generator of positive integers iterator
function* posIntGenerator(){
var value = 0;
while (true){
yield value++;
}
}
var posIntIterator = posIntGenerator();
// New expression syntax below
var posIntBy2Iterator = (anInt * 2 for (anInt in posIntIterator));
posIntBy2Iterator.next(); // 0
posIntBy2Iterator.next(); // 2
posIntBy2Iterator.next(); // 4
posIntBy2Iterator.next(); // 6
Arrow function expressions
are like function expressions
with shorter syntax
+ it binds this
value
// This is an anomous function
function(){};
// No parameter, returns undefined
() => {};
// No parameter, returns "foo"
() => "foo"
// Single parameter, returns double
a => a * 2
// Two parameters, returns sum
(a, b) => a + b
There was no built-in module concept in JavaScript
...but two standards :
...unfortunately they are not compatible
ES6 introduces another format
Trying to fill the gap
Module definition
// calculator/lib/calc.js
const A_NOT_EXPORTED_CONSTANT = 7;
function multiply(x) {
return x * A_NOT_EXPORTED_CONSTANT;
}
// Public object exported
module.exports = {
multiply: multiply
};
Module import
// calculator/main.js
import { multiply } from 'lib/calc';
console.log(multiply(3));
Plus the new <module>
tag for the browser
Example from http://www.2ality.com/2013/07/es6-modules.html
Class
keyword but still Prototype
inheritance
Sugar syntax to replace framework machinery
class Gentleman extends Dude {
constructor(name) {
super(name);
this.sayHello();
}
sayHello() {
console.log('Hello Sir');
};
}
}
Expression expands in places
where multiple elements are expected
function sum(a, b, c, d) {
return a + b + c + d;
}
var expandable = [4, 12, 7, 19];
sum(...expandable); // 42
var expandable2 = [-5, 8];
sum(1, ...expandable2, 20); // 24
var expandable3 = [...expandable, ...expandable2];
//[4, 12, 7, 19, -5, 8]
const
& let
Brings block scope variables and constantes
if (x) {
let foo;
const bar = 10;
// `foo` and `bar` are defined inside the if block
bar = 11; // Error
//the value of `bar` cannot change
}
You (may) used to write
function f(name, age) {
age = age || 18;
}
You will write
function f(name, age = 18) {
}
Representation of an indefinite number
of tailing arguments as an array
function sum(a, b, ...args) {
//args is an Array
var restSum = args.reduce(function(prev, current){
return prev + current;
}, 0);
return a + b + restSum;
}
sum(1, 2); // 3
sum(1, 2, 8); // 11
sum(1, 2, 8, 12); // 23
Until now promises were provided by libraries
( jQuery, Q, Bluebird)
Promises are built-in ES6, they :
Proxies can change the default behavior
of an object
var proxyTraps = {
get: function(obj, prop) {
return 42;
}
};
var person = new Proxy({ age : 100 }, proxyTraps);
console.log(person.age); // 42
person.age = 18;
console.log(person.age); // 42
The proxyTraps.get
is a 'trap' method
There are many others available
Useful for data validation, sanitization, dynamic code analysis...
Maps and WeakMaps are key/value maps
Where keys are objects
≠ JS native object : keys are string
var wm = new WeakMap(),
trivialeObject = {
'Clermont' : 'Ferrand'
};
wm.set(trivialeObject, 42);
wm.get(trivialeObject); // 42
wm.has(trivialeObject); // true
wm.delete(trivialeObject);
wm.has(trivialeObject); // false
An example from jQuery with AJAX calls
$.getJSON('customers.json', function(customers){
$.getJSON('products.json', function(products){
console.log('We have ' + products.length + ' products');
console.log('and' + customers.length + ' customers');
});
});
//Synchronous sequence of async calls : bad pattern
var customersPromise = $.getJSON('customers.json'); //Returns a promise
var productsPromise = $.getJSON('products.json'); //Returns another promise
$.when(customersPromise, productsPromise)
.then(function(customers, products){
// Called only if both promises are resolved
console.log('We have ' + products.length + ' products');
console.log('and' + customers.length + ' customers');
});
ES7 is coming next...
In your shell with NodeJS,
in your OS with Windows Store App or Node webkit,
in your mobile with PhoneGap, FirefoxOS, Tizen,
in your things with Arduino or BeagleBone,
in the code compiled from C++, Java, C#... via Emcripten,
and in your websites, back and front, with a huge ecosystem.
Don't forget to type mdn
when Googling
Our community