ES6 = ECMAScript 6 = ECMAScript 2015 = La version del estándar de implementación de Javascript ratificada en Junio del 2015
Código fuente Código para las Máquinas
Código Fuente Otro Código Fuente
var data;let data;
const data; // para constantesconsole.log(x + ', ' + y);console.log(`${x}, ${y}`)var HTML5_SKELETON =
'<!doctype html>\n' +
'<html>\n' +
'<head>\n' +
' <meta charset="UTF-8">\n' +
' <title></title>\n' +
'</head>\n' +
'<body>\n' +
'</body>\n' +
'</html>\n';
const HTML5_SKELETON = `
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
</html>`;function showModal() {
var that = this;
$('#container').on('click', function() {
that.updatePosition();
});
}function showModal() {
$('#container').on('click', () => {
this.updatePosition();
});
}var fecha = [2015, 10, 23];
var dia = fecha[2]
var page = { impresiones: 23, unicas: 5 };
var unicas = page.unicas;let fecha = [2015, 10, 23];
let [ , , dia] = fecha;
const page = { impresiones: 23, unicas: 5 };
{ , unicas } = page; // esto no funciona
// Destructuring aquí, funciona a la "pick what you want"
let { unicas: misVisitasUnicas } = page; // ahora `misVisitasUnicas` esta declarada
let { unicas } = page; // ahora `unicas` esta declaradavar arr = ['a', 'b', 'c'];
for (var i=0; i<arr.length; i++) {
var elem = arr[i];
console.log(elem);
}let arr = ['a', 'b', 'c'];
for (let elem of arr) {
console.log(elem);
}
// si ocupas acceder al índice
let arr = ['a', 'b', 'c'];
for (let [index, elem] of arr) {
console.log(elem);
}function foo(x, y) {
x = x || 0;
y = y || 0;
···
}function foo(x=0, y=0) {
···
}function selectEntries(options) {
var start = options.start || 0;
var end = options.end || -1;
···
}function selectEntries({ start=0, end=-1 }) {
···
}
function selectEntries(
{ start=0, end=-1 } = {}
) {
···
}function logAllArguments() {
for (var i=0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
function logAllArguments(...args) {
for (let arg of args) {
console.log(arg);
}
}> Math.max.apply(null, [-1, 5, 11, 3])
> 11
new Date(2015, 10, 23);> Math.max(...[-1, 5, 11, 3])
> 11
> Math.max(-1, ...[-1, 5, 11], 3)
> 11
let arr1 = ['a', 'b'];
let arr2 = ['c', 'd'];
arr1.push(...arr2);
// arr1 is now ['a', 'b', 'c', 'd']
let newArr = [...arr1, ...arr2];
new Date(...[1912, 11, 24])The spread operator turns the elements of an Array into arguments of a function call or into elements of an Array literal.
function Person(name) {
this.name = name;
}
Person.prototype.describe = function () {
return 'Person called '+this.name;
};class Person {
constructor(name) {
this.name = name;
}
describe() {
return 'Person called '+this.name;
}
}Math.max.apply(null, [-1, 5, 11, 3])
> 11Math.max(...[-1, 5, 11, 3])
11class Car {
constructor(passengers) {
this.passengers = passengers;
}
claxon(state) {
//...
}
}
class Jeep extends Car {
constructor() {
super();
}
}