@Vitormalencar
+Vitor Alencar
ES -Harmony Proposta
ES 6 rascunho da especificação
ES 6 Candidate
ECMA-262 Ed.6 standardized
//ES5
var y = function(x) {
return x + 1;
}
//ES6
var y = (x) => x + 1
//ES6
let x = (x) =>
{return x + 1}
let x = (x, y) =>
({
x: x,
y: y
})
//ES5
var x = function(x) {
return x + 1;
}
var x = function(x, y) {
return {
x: x,
y: y
};
}
//ES5
var obj = {
doIt: function(){},
handle: function(){
document.addEventListener(‘click’, function(e) {
this.doIt();
}.bind(this));
}
}
//ES6
var obj = {
doIt: function(){},
handle: function(){
document.addEventListener(‘click’,
(e) => this.doIt());
}
}
function Student(data){
this.name = data.name || "Jon Doe";
this.age = data.age>=0 ? data.age : -1;
this.getInfo = function () {
return this.name + ", " + this.age;
};
this.sayHi = function () {
window.setTimeout( function () {
console.log( this );
}, 100 );
}
}
let mary = new Student({
name: "Mary Lou",
age: 13
});
console.log( mary.getInfo() ); // "Mary Lou, 13"
mary.sayHi();
// window
function Student(data){
this.name = data.name || "Jon Doe";
this.age = data.age>=0 ? data.age : -1;
this.getInfo = function () {
return this.name + ", " + this.age;
};
this.sayHi = function () {
window.setTimeout( ()=>{ // a única diferença está aqui
console.log( this );
}, 100 );
}
}
let mary = new Student({
name: "Mary Lou",
age: 13
});
console.log( mary.getInfo() ); // "Mary Lou, 13"
mary.sayHi();
// Object { name: "Mary Lou", age: 13, ... }
npm install --global babel