SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
"Agora Nervoso"
"Agora Nervoso"
Luís Fernando VendrameLuís Fernando Vendrame
lvendrame@gmail.com
@lvendrame
O que era bom ficouO que era bom ficou
melhor ainda!melhor ainda!
ECMAScript 2015ECMAScript 2015
let, const, class
modules (export, import)
Destructing arrays e objects
Spread operator
promises (nativo)
Arrow functions
Generators + coroutines
Default parameter values
String templates
Property e método shorthand
Lembra do Hooking
Let
Const
ConstConst
const myConst = "Meu valor";
myConst = "Novo valor"; //Uncaught TypeError: Assignment to constant varia
//But
const myObj = { prop: 'valor' };
myObj.prop = "Novo valor";// Vai de boas
LetLet
function main(){
let a = 10;
for(let i = 0; i < a; i++){
let d = a + i;
}
console.log(d);//Uncaught ReferenceError: d is not defin
}
ClassClass
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
}
}
Arrow FunctionsArrow Functions
x => x * 2;
(x, y) => x + y;
odds = evens.map(v => v + 1);
pairs = evens.map(v => ({ even: v, odd: v + 1 }));
nums = evens.map((v, i) => v + i);
Destructuring ArrayDestructuring Array
var list = [ 1, 2, 3 ];
var [ a, , b ] = list; //a = 1, b = 3
[ b, a ] = [ a, b ];
Destructuring ObjectDestructuring Object
function getASTNode(){
return {
op: "Olá",
omg: "Nem",
rhs: 35,
lhs: 26,
children: []
};
}
var { op, lhs, rhs } = getASTNode();
ModulesModules
// lib/math.js
export function sum (x, y) { return x + y }
export var pi = 3.141593
// someApp.js
import * as math from "lib/math"
console.log("2π = " + math.sum(math.pi, math.pi))
// otherApp.js
import { sum, pi } from "lib/math"
console.log("2π = " + sum(pi, pi))
Spread OperatorSpread Operator
var params = [ "hello", true, 7 ]
var other = [ 1, 2, ...params ] // [ 1, 2, "hello", true, 7 ]
//Function with Rest Parameter
function f (x, y, ...a) {
return (x + y) * a.length
}
f(1, 2, ...params) === 9
var str = "foo"
var chars = [ ...str ] // [ "f", "o", "o" ]
PromisesPromises
Agora existe uma implementação nativa
compatível com Promises A+.
 
“
Mas ainda prefiro o Bluebird
IteratorsIterators
{
next: function(){
return {
value: ???,
done: true|false;
}
}
myObject[Symbol.iterator]
for(let value of myObject);
//Exemplo
function makeIterator(array){
var nextIndex = 0;
return {
next: function(){
return nextIndex < array.length ?
{value: array[nextIndex++], done: false} :
{done: true};
}
}
}
GeneratorsGenerators
function *contar(max){
let i = 0;
while(i<max){
yield i;
i++;
}
return i;
}
var contar10 = contar(10);
console.log(contar.next());//{value: 0, done: false}
console.log(contar.next());//{value: 1, done: false}
console.log(contar.next());//{value: 2, done: false}
console.log(contar.next());//{value: 3, done: false}
console.log(contar.next());//{value: 4, done: false}
console.log(contar.next());//{value: 5, done: false}
console.log(contar.next());//{value: 6, done: false}
console.log(contar.next());//{value: 7, done: false}
console.log(contar.next());//{value: 8, done: false}
console.log(contar.next());//{value: 9, done: false}
console.log(contar.next());//{value: 10, done: true}
console.log(contar.next());//{value: undefined, done: tru
GeneratorsGenerators
function* foo(x) {
var y = 2 * (yield (x + 1));
var z = yield (y / 3);
return (x + y + z);
}
var it = foo( 5 );
console.log( it.next() ); // { value:6, done:false }
console.log( it.next( 12 ) ); // { value:8, done:false }
console.log( it.next( 13 ) ); // { value:42, done:true }
CoroutinesCoroutines
'use strict'
const co = require('co');
const marked = require('marked');
const fs = require('fs-promise');
const handlebars = require('handlebars');
co(function *() {
let md = yield fs.readFile('README.md');
let html = marked(md.toString());
let template = yield fs.readFile('layout.hbs');
let output = handlebars.compile(template.toString())({
title: 'README',
contents: html
});
yield fs.writeFile('index.html', output);
}).catch(function(err) {
console.error(err.stack);
});
Default parameter valueDefault parameter value
function f (x, y = 7, z = 42) {
return x + y + z
}
f(1) === 50
//ECMAScript 5
function f (x, y, z) {
y = y || 7;
if (z === undefined){
z = 42;
}
return x + y + z;
};
f(1) === 50;
String templateString template
var customer = { name: "Foo" };
var card = { amount: 7, product: "Bar", unitprice: 42 };
var message = `Hello ${customer.name},
want to buy ${card.amount} ${card.product} for
a total of ${card.amount * card.unitprice} bucks?`;
Property & method shorthandProperty & method shorthand
var x = 10;
var y = "eita";
var obj = { x, y };// { x: 10, y: "eita" }
let otherObj = {
foo (a, b) {
…
},
bar (x, y) {
…
},
*quux (x, y) {
…
}
};
ReferênciasReferências
http://es6-features.org/
https://developer.mozilla.org/bm/docs/Web/JavaScript
https://davidwalsh.name/es6-generators
http://tobyho.com/2015/12/27/promise-based-coroutines-nodejs/
https://x.st/javascript-coroutines/
GO BACK

Más contenido relacionado

La actualidad más candente

completion_proc and history
completion_proc and historycompletion_proc and history
completion_proc and history
Nobuhiro IMAI
 
「Frama-Cによるソースコード検証」 (mzp)
「Frama-Cによるソースコード検証」 (mzp)「Frama-Cによるソースコード検証」 (mzp)
「Frama-Cによるソースコード検証」 (mzp)
Hiroki Mizuno
 

La actualidad más candente (20)

20151224-games
20151224-games20151224-games
20151224-games
 
Cpp
Cpp Cpp
Cpp
 
Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018
 
The Big Three
The Big ThreeThe Big Three
The Big Three
 
completion_proc and history
completion_proc and historycompletion_proc and history
completion_proc and history
 
C++ programs
C++ programsC++ programs
C++ programs
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
「Frama-Cによるソースコード検証」 (mzp)
「Frama-Cによるソースコード検証」 (mzp)「Frama-Cによるソースコード検証」 (mzp)
「Frama-Cによるソースコード検証」 (mzp)
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
 
谈谈Javascript设计
谈谈Javascript设计谈谈Javascript设计
谈谈Javascript设计
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generators
 
Reactive x
Reactive xReactive x
Reactive x
 
Rcpp11 genentech
Rcpp11 genentechRcpp11 genentech
Rcpp11 genentech
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
Vcs23
Vcs23Vcs23
Vcs23
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
vbscript-reference book
vbscript-reference bookvbscript-reference book
vbscript-reference book
 
Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223
 
Groovy
GroovyGroovy
Groovy
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 

Similar a JavaScript - Agora nervoso

HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
JavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your codeJavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your code
Laurence Svekis ✔
 

Similar a JavaScript - Agora nervoso (20)

FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
ES6, WTF?
ES6, WTF?ES6, WTF?
ES6, WTF?
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
ESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. NowESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. Now
 
Internal workshop es6_2015
Internal workshop es6_2015Internal workshop es6_2015
Internal workshop es6_2015
 
Introduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy CresineIntroduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy Cresine
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
JavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your codeJavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your code
 
The State of JavaScript (2015)
The State of JavaScript (2015)The State of JavaScript (2015)
The State of JavaScript (2015)
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
Javascript
JavascriptJavascript
Javascript
 
Day 1
Day 1Day 1
Day 1
 
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMD
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

JavaScript - Agora nervoso

  • 2. Luís Fernando VendrameLuís Fernando Vendrame lvendrame@gmail.com @lvendrame
  • 3. O que era bom ficouO que era bom ficou melhor ainda!melhor ainda!
  • 4. ECMAScript 2015ECMAScript 2015 let, const, class modules (export, import) Destructing arrays e objects Spread operator promises (nativo) Arrow functions Generators + coroutines Default parameter values String templates Property e método shorthand
  • 6. ConstConst const myConst = "Meu valor"; myConst = "Novo valor"; //Uncaught TypeError: Assignment to constant varia //But const myObj = { prop: 'valor' }; myObj.prop = "Novo valor";// Vai de boas
  • 7. LetLet function main(){ let a = 10; for(let i = 0; i < a; i++){ let d = a + i; } console.log(d);//Uncaught ReferenceError: d is not defin }
  • 8. ClassClass 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 } }
  • 9. Arrow FunctionsArrow Functions x => x * 2; (x, y) => x + y; odds = evens.map(v => v + 1); pairs = evens.map(v => ({ even: v, odd: v + 1 })); nums = evens.map((v, i) => v + i);
  • 10. Destructuring ArrayDestructuring Array var list = [ 1, 2, 3 ]; var [ a, , b ] = list; //a = 1, b = 3 [ b, a ] = [ a, b ];
  • 11. Destructuring ObjectDestructuring Object function getASTNode(){ return { op: "Olá", omg: "Nem", rhs: 35, lhs: 26, children: [] }; } var { op, lhs, rhs } = getASTNode();
  • 12. ModulesModules // lib/math.js export function sum (x, y) { return x + y } export var pi = 3.141593 // someApp.js import * as math from "lib/math" console.log("2π = " + math.sum(math.pi, math.pi)) // otherApp.js import { sum, pi } from "lib/math" console.log("2π = " + sum(pi, pi))
  • 13. Spread OperatorSpread Operator var params = [ "hello", true, 7 ] var other = [ 1, 2, ...params ] // [ 1, 2, "hello", true, 7 ] //Function with Rest Parameter function f (x, y, ...a) { return (x + y) * a.length } f(1, 2, ...params) === 9 var str = "foo" var chars = [ ...str ] // [ "f", "o", "o" ]
  • 14. PromisesPromises Agora existe uma implementação nativa compatível com Promises A+.   “ Mas ainda prefiro o Bluebird
  • 15. IteratorsIterators { next: function(){ return { value: ???, done: true|false; } } myObject[Symbol.iterator] for(let value of myObject); //Exemplo function makeIterator(array){ var nextIndex = 0; return { next: function(){ return nextIndex < array.length ? {value: array[nextIndex++], done: false} : {done: true}; } } }
  • 16. GeneratorsGenerators function *contar(max){ let i = 0; while(i<max){ yield i; i++; } return i; } var contar10 = contar(10); console.log(contar.next());//{value: 0, done: false} console.log(contar.next());//{value: 1, done: false} console.log(contar.next());//{value: 2, done: false} console.log(contar.next());//{value: 3, done: false} console.log(contar.next());//{value: 4, done: false} console.log(contar.next());//{value: 5, done: false} console.log(contar.next());//{value: 6, done: false} console.log(contar.next());//{value: 7, done: false} console.log(contar.next());//{value: 8, done: false} console.log(contar.next());//{value: 9, done: false} console.log(contar.next());//{value: 10, done: true} console.log(contar.next());//{value: undefined, done: tru
  • 17. GeneratorsGenerators function* foo(x) { var y = 2 * (yield (x + 1)); var z = yield (y / 3); return (x + y + z); } var it = foo( 5 ); console.log( it.next() ); // { value:6, done:false } console.log( it.next( 12 ) ); // { value:8, done:false } console.log( it.next( 13 ) ); // { value:42, done:true }
  • 18. CoroutinesCoroutines 'use strict' const co = require('co'); const marked = require('marked'); const fs = require('fs-promise'); const handlebars = require('handlebars'); co(function *() { let md = yield fs.readFile('README.md'); let html = marked(md.toString()); let template = yield fs.readFile('layout.hbs'); let output = handlebars.compile(template.toString())({ title: 'README', contents: html }); yield fs.writeFile('index.html', output); }).catch(function(err) { console.error(err.stack); });
  • 19. Default parameter valueDefault parameter value function f (x, y = 7, z = 42) { return x + y + z } f(1) === 50 //ECMAScript 5 function f (x, y, z) { y = y || 7; if (z === undefined){ z = 42; } return x + y + z; }; f(1) === 50;
  • 20. String templateString template var customer = { name: "Foo" }; var card = { amount: 7, product: "Bar", unitprice: 42 }; var message = `Hello ${customer.name}, want to buy ${card.amount} ${card.product} for a total of ${card.amount * card.unitprice} bucks?`;
  • 21. Property & method shorthandProperty & method shorthand var x = 10; var y = "eita"; var obj = { x, y };// { x: 10, y: "eita" } let otherObj = { foo (a, b) { … }, bar (x, y) { … }, *quux (x, y) { … } };