cansei de ser sexy
vs
vs
vs
vs
vs
vs
vs
vs
vs
ou ES2015
const PI = 3.141593
// ...
function f() {
{
let x;
{
// okay, block scoped name
const x = "sneaky";
// error, const
x = "foo";
}
// error, already declared in block
let x = "inner";
}
}
class Shape {
constructor (id, x, y) {
this.id = id
this.move(x, y)
}
move (x, y) {
this.x = x
this.y = y
}
}
class Rectangle extends Shape {
constructor (id, x, y, width, height) {
super(id, x, y)
this.width = width
this.height = height
}
}
class Circle extends Shape {
constructor (id, x, y, radius) {
super(id, x, y)
this.radius = radius
}
}
// Encurtar nomenclatura
var a = [
"Hidrogenio",
"Helio",
"Litio",
"Berilio"
];
var a2 = a.map(function(s){ return s.length });
var a3 = a.map( s => s.length );
// this léxico ou lexical this
// modo antigo
function Pessoa() {
var self = this; // Alguns escolhem `that` ao invés de `self`.
// Escolha uma e seja consistente.
self.idade = 0;
setInterval(function crescer() {
// O callback referência a variável `self` a qual
// o valor é o objeto esperado.
self.idade++;
}, 1000);
}
var p = new Pessoa();
// com ES6
function Pessoa(){
this.idade = 0;
setInterval(() => {
this.idade++; // |this| corretamente referência ao objeto Pessoa
}, 1000);
}
var p = new Pessoa();
// Sem template String
let link = 'http://www.ecma-international.org/ecma-262/6.0/';
let cssClass = 'link-js';
let text = 'ECMAScript® 2015 Language Specification';
let elementTpl = '<a href="' + link + '" class="' + cssClass + '">' + text + '</a>';
console.log(elementTpl);
// <a href="http://..." class="link-js">ECMAScript® 2015...</a>
// Com template String
elementTpl = `<a href="${link}" class="${cssClass}">${text}</a>`;
console.log(elementTpl);
// <a href="http://..." class="link-js">ECMAScript® 2015...</a>
//sum.js
var sum = function(x, y) {
return x + y;
};
export { sum }
//multiplier.js
var multiplier = function(x, y) {
return x * y;
};
export { multiplier }
//calculator.js
import { sum } from "./sum";
import { multiplier } from "./multiplier";
var result = sum(15,27); // 42
result = multiplier(6,7); // 42
welcome promises hell hehe
var promise = new Promise((resolve, reject) => {
if (/* Condição */) {
resolve("Promise + Arrow Functions = <3");
}
else {
reject(Error("Oops!"));
}
});
promise.then((result) => {
console.log(result); // "Promise + Arrow Functions = <3"
}, (err) => {
console.log(err); // Error: "Oops!"
});
Number.EPSILON
Number.isInteger(Infinity) // false
Number.isNaN("NaN") // false
Math.acosh(3) // 1.762747174039086
Math.hypot(3, 4) // 5
Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2
"abcde".includes("cd") // true
"abc".repeat(3) // "abcabcabc"
Array.from(document.querySelectorAll('*')) // Returns a real Array
Array.of(1, 2, 3) // Similar to new Array(...), but without special one-arg behavior
[0, 0, 0].fill(7, 1) // [0,7,7]
[1, 2, 3].find(x => x == 3) // 3
[1, 2, 3].findIndex(x => x == 2) // 1
[1, 2, 3, 4, 5].copyWithin(3, 0) // [1, 2, 3, 1, 2]
["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
["a", "b", "c"].keys() // iterator 0, 1, 2
["a", "b", "c"].values() // iterator "a", "b", "c"
Object.assign(Point, { origin: new Point(0,0) })
Cezar Luiz
cezarluiz.c@gmail.com
github@CezarLuiz0