SlideShare uma empresa Scribd logo
1 de 128
Baixar para ler offline
revisão
Object	

!
Object	

!
Prototipo de Object	

!
Object	

!
Prototipo de Object	

!
Object.prototype.pO = 1
Object	

!
Prototipo de Object	

!
pO = 1
Object.prototype.pO = 1
A	

!
a = 2
var A = function() {
this.a = 2;
}
Object	

!
Prototipo de Object	

!
pO = 1
A	

!
a = 2
Prototipo de A	

!
Object	

!
Prototipo de Object	

!
pO = 1
A	

!
a = 2
Prototipo de A	

!
A.prototype.pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
A	

!
a = 2
Prototipo de A	

!
pA = 3
A.prototype.pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
var B = function() {
this.b = 4;
}
B	

!
b = 4
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
Prototipo de B	

!
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
Prototipo de B	

!
B.prototype = new A
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
B.prototype = new A
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
B.prototype = new A
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
B.prototype = new A
new A()	

!
a = 2
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
B.prototype = new A
new A()	

!
a = 2
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2
B.prototype.pB = 5
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
B.prototype.pB = 5
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x = new B()
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x = new B()
x	

new B()	

!
b = 4
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x = new B()
x	

new B()	

!
b = 4
Prototipo de B	

!
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x	

new B()	

!
b = 4
Prototipo de B	

!
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x.b
x	

new B()	

!
b = 4
Prototipo de B	

!
?
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x	

new B()	

!
b = 4
Prototipo de B	

!
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x.pB
x	

new B()	

!
b = 4
Prototipo de B	

!
?
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x	

new B()	

!
b = 4
Prototipo de B	

!
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x.a
x	

new B()	

!
b = 4
Prototipo de B	

!
?
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x	

new B()	

!
b = 4
Prototipo de B	

!
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x.pA
x	

new B()	

!
b = 4
Prototipo de B	

!
?
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x	

new B()	

!
b = 4
Prototipo de B	

!
A	

!
a = 2
Prototipo de A	

!
pA = 3
Object	

!
Prototipo de Object	

!
pO = 1
B	

!
b = 4
new A()	

!
a = 2	

pB = 5
x.pO
x	

new B()	

!
b = 4
Prototipo de B	

!
?
function f (x) {
return function (y) {
return x + y;
};
}
var a = f(5);
var b = a(3);
alert(b); // ????
Javascript
Alexandre Gomes
princípios da programação funcional com
programação imperativa
1. Bata bem todos os ingredientes
( menos o fermento) da massa no
liquidificador, aproximadamente
2 a 3 minutos	

2. Acrescente o fermento e bata por
mais uns 15 segundos	

3. Coloque em uma fôrma redonda,
u n t a d a c o m m a n t e i g a e
polvilhada com farinha de trigo	

4. Asse por cerca de 40 minutos em
forno médio (180º graus),
preaquecido
programação declarativa
Comunique assim que estiver
pronto.	

Não deixe o bolo queimar.	

Não deixe que abram o forno
antes da hora.
Buscar alunos com menos de 30 anos.
alunos = [{nome: “Rodrigo”, idade: 30},
{nome: “Patricia”, idade: 20},
{nome: “Marcos”, idade: 33},
{nome: “Ricardo”, idade: 28}
]
menores = []
!
for (int i = 1; i <= 4; i++) {
aluno = alunos[i];
if (aluno.idade < 30) {
menores.add(aluno);
}
}
!
return menores;
alunos = [{nome: “Rodrigo”, idade: 30},
{nome: “Patricia”, idade: 20},
{nome: “Marcos”, idade: 33},
{nome: “Ricardo”, idade: 28}
]
menores = []
!
for (int i = 1; i <= 4; i++) {
aluno = alunos[i];
if (aluno.idade < 30) {
menores.add(aluno);
}
}
!
return menores;
alunos = [{nome: “Rodrigo”, idade: 30},
{nome: “Patricia”, idade: 20},
{nome: “Marcos”, idade: 33},
{nome: “Ricardo”, idade: 28}
]
imperativa
alunos = [{nome: “Rodrigo”, idade: 30},
{nome: “Patricia”, idade: 20},
{nome: “Marcos”, idade: 33},
{nome: “Ricardo”, idade: 28}
]
Nome Idade
1 Rodrigo 30
2 Patricia 20
3 Marcos 33
4 Ricardo 28
Nome Idade
1 Rodrigo 30
2 Patricia 20
3 Marcos 33
4 Ricardo 28
SELECT * FROM alunos WHERE idade < 30
Nome Idade
1 Rodrigo 30
2 Patricia 20
3 Marcos 33
4 Ricardo 28
SELECT * FROM alunos WHERE idade < 30
declarativa
menores = []
!
for (int i = 1; i <= 4; i++) {
aluno = alunos[i];
if (aluno.idade < 30) {
menores.add(aluno);
}
}
!
return menores;
alunos = [{nome: “Rodrigo”, idade: 30},
{nome: “Patricia”, idade: 20},
{nome: “Marcos”, idade: 33},
{nome: “Ricardo”, idade: 28}
]
SELECT * FROM alunos WHERE idade < 30
var numbers = [1,2,3,4,5]
var numbers = [1,2,3,4,5]
var doubled = []
!
for(var i = 0; i < numbers.length; i++) {
var newNumber = numbers[i] * 2
doubled.push(newNumber)
}
console.log(doubled) //=> [2,4,6,8,10]
var numbers = [1,2,3,4,5]
var doubled = []
!
for(var i = 0; i < numbers.length; i++) {
var newNumber = numbers[i] * 2
doubled.push(newNumber)
}
console.log(doubled) //=> [2,4,6,8,10]
var numbers = [1,2,3,4,5]
var doubled = numbers.map(function(n) {
return n * 2
})
console.log(doubled) //=> [2,4,6,8,10]
var numbers = [1,2,3,4,5]!
!
!
!
!
!
var numbers = [1,2,3,4,5]!
var total = 0!
!
for(var i = 0; i < numbers.length; i++) {!
total += numbers[i]!
}!
console.log(total) //=> 15
var numbers = [1,2,3,4,5]!
var total = 0!
!
for(var i = 0; i < numbers.length; i++) {!
total += numbers[i]!
}!
console.log(total) //=> 15
var numbers = [1,2,3,4,5]!
!
var total = numbers.reduce(function(sum,n){!
return sum + n!
});!
console.log(total) //=> 15
programação imperativa
1. HOW
programação declarativa
WHAT
SELECT * FROM alunos WHERE idade < 30
SELECT * FROM alunos WHERE idade < 30
var doubled = [1,2,3].map(function(n) {
return n * 2
})
SELECT * FROM alunos WHERE idade < 30
var doubled = [1,2,3].map(function(n) {
return n * 2
})
var total = [1,2,3].reduce(function(sum,n){!
return sum + n!
});
function f (x) {
return function (y) {
return x + y;
};
}
var a = f(5);
var b = a(3);
alert(b); // ????
first-class
functions
higher-order
function
:closures:
a closure (...) is a
function or reference to a
function together with a
referencing environment
“
http://en.wikipedia.org/wiki/Closure_(computer_science)
:closure:
Por quê?!
#comofas?!
a closure (...) is a
function or reference to a
function together with a
referencing environment
“
closure = function + environment
closure = function + environment
(
=
=
=
)
closure = function + environment
...
E o Kiko ?!
encapsulamento,
que é bom....
>> “Alexandre Gomes”
>> undefined
>> undefined
Yahoo Javascript
Module Pattern
P&Raprender.unb.br
Programação Funcional com Javascript

Mais conteúdo relacionado

Destaque

Consejos para que_tengas_en_cuenta
Consejos para que_tengas_en_cuentaConsejos para que_tengas_en_cuenta
Consejos para que_tengas_en_cuentamnugiralt
 
Web Usability, Consistency, and Content Development 2015 Code(Her)-Hou-Tobolsky
Web Usability, Consistency, and Content Development 2015 Code(Her)-Hou-TobolskyWeb Usability, Consistency, and Content Development 2015 Code(Her)-Hou-Tobolsky
Web Usability, Consistency, and Content Development 2015 Code(Her)-Hou-TobolskyWeimin Hou
 
Evaluación ecológica rápida (1)
Evaluación ecológica rápida (1)Evaluación ecológica rápida (1)
Evaluación ecológica rápida (1)justiniano123
 
Plano de Ensino de Tópicos Avançados em Engenharia de Software
Plano de Ensino de Tópicos Avançados em Engenharia de SoftwarePlano de Ensino de Tópicos Avançados em Engenharia de Software
Plano de Ensino de Tópicos Avançados em Engenharia de SoftwareAlexandre Gomes
 
Usability for Web Designers
Usability for Web DesignersUsability for Web Designers
Usability for Web DesignersBen Woods
 

Destaque (7)

Consejos para que_tengas_en_cuenta
Consejos para que_tengas_en_cuentaConsejos para que_tengas_en_cuenta
Consejos para que_tengas_en_cuenta
 
Saowalak mook..
Saowalak  mook..Saowalak  mook..
Saowalak mook..
 
Web Usability, Consistency, and Content Development 2015 Code(Her)-Hou-Tobolsky
Web Usability, Consistency, and Content Development 2015 Code(Her)-Hou-TobolskyWeb Usability, Consistency, and Content Development 2015 Code(Her)-Hou-Tobolsky
Web Usability, Consistency, and Content Development 2015 Code(Her)-Hou-Tobolsky
 
Evaluación ecológica rápida (1)
Evaluación ecológica rápida (1)Evaluación ecológica rápida (1)
Evaluación ecológica rápida (1)
 
Plano de Ensino de Tópicos Avançados em Engenharia de Software
Plano de Ensino de Tópicos Avançados em Engenharia de SoftwarePlano de Ensino de Tópicos Avançados em Engenharia de Software
Plano de Ensino de Tópicos Avançados em Engenharia de Software
 
Usability for Web Designers
Usability for Web DesignersUsability for Web Designers
Usability for Web Designers
 
Tp Eliseo Veron
Tp Eliseo VeronTp Eliseo Veron
Tp Eliseo Veron
 

Mais de Alexandre Gomes

Construção de Software - 1º/2017
Construção de Software - 1º/2017Construção de Software - 1º/2017
Construção de Software - 1º/2017Alexandre Gomes
 
UnB/PPCA/CS2016 - Projeto 2
UnB/PPCA/CS2016 - Projeto 2UnB/PPCA/CS2016 - Projeto 2
UnB/PPCA/CS2016 - Projeto 2Alexandre Gomes
 
Javascript do jeito certo
Javascript do jeito certoJavascript do jeito certo
Javascript do jeito certoAlexandre Gomes
 
Construção de Software - 1º/2016
Construção de Software - 1º/2016Construção de Software - 1º/2016
Construção de Software - 1º/2016Alexandre Gomes
 
Construção de Software - 1º semestre de 2014
Construção de Software - 1º semestre de 2014Construção de Software - 1º semestre de 2014
Construção de Software - 1º semestre de 2014Alexandre Gomes
 
Laboratório de Métodos Ágeis 1/2014 - Apresentação
Laboratório de Métodos Ágeis 1/2014 - ApresentaçãoLaboratório de Métodos Ágeis 1/2014 - Apresentação
Laboratório de Métodos Ágeis 1/2014 - ApresentaçãoAlexandre Gomes
 
OpenData, Web Semântica e afins.
OpenData, Web Semântica e afins.OpenData, Web Semântica e afins.
OpenData, Web Semântica e afins.Alexandre Gomes
 
Javascript do jeito certo
Javascript do jeito certoJavascript do jeito certo
Javascript do jeito certoAlexandre Gomes
 
Javascript orientado a testes
Javascript orientado a testesJavascript orientado a testes
Javascript orientado a testesAlexandre Gomes
 

Mais de Alexandre Gomes (20)

Construção de Software - 1º/2017
Construção de Software - 1º/2017Construção de Software - 1º/2017
Construção de Software - 1º/2017
 
TDDing com Javascript
TDDing com JavascriptTDDing com Javascript
TDDing com Javascript
 
Aprendendo a Aprender
Aprendendo a AprenderAprendendo a Aprender
Aprendendo a Aprender
 
UnB/PPCA/CS2016 - Projeto 2
UnB/PPCA/CS2016 - Projeto 2UnB/PPCA/CS2016 - Projeto 2
UnB/PPCA/CS2016 - Projeto 2
 
Javascript do jeito certo
Javascript do jeito certoJavascript do jeito certo
Javascript do jeito certo
 
Construção de Software - 1º/2016
Construção de Software - 1º/2016Construção de Software - 1º/2016
Construção de Software - 1º/2016
 
O Pensamento Ágil
O Pensamento ÁgilO Pensamento Ágil
O Pensamento Ágil
 
Manifesto 2.0
Manifesto 2.0Manifesto 2.0
Manifesto 2.0
 
Construção de Software - 1º semestre de 2014
Construção de Software - 1º semestre de 2014Construção de Software - 1º semestre de 2014
Construção de Software - 1º semestre de 2014
 
Business Modeling
Business ModelingBusiness Modeling
Business Modeling
 
Design Thinking
Design ThinkingDesign Thinking
Design Thinking
 
Manifesto Ágil
Manifesto ÁgilManifesto Ágil
Manifesto Ágil
 
Laboratório de Métodos Ágeis 1/2014 - Apresentação
Laboratório de Métodos Ágeis 1/2014 - ApresentaçãoLaboratório de Métodos Ágeis 1/2014 - Apresentação
Laboratório de Métodos Ágeis 1/2014 - Apresentação
 
Scraping by examples
Scraping by examplesScraping by examples
Scraping by examples
 
Scraping by examples
Scraping by examplesScraping by examples
Scraping by examples
 
OpenData, Web Semântica e afins.
OpenData, Web Semântica e afins.OpenData, Web Semântica e afins.
OpenData, Web Semântica e afins.
 
Javascript do jeito certo
Javascript do jeito certoJavascript do jeito certo
Javascript do jeito certo
 
Javascript orientado a testes
Javascript orientado a testesJavascript orientado a testes
Javascript orientado a testes
 
Escolhas 2.0
Escolhas 2.0Escolhas 2.0
Escolhas 2.0
 
Computacao Invisivel
Computacao InvisivelComputacao Invisivel
Computacao Invisivel
 

Programação Funcional com Javascript

  • 1.
  • 6. Object ! Prototipo de Object ! pO = 1 Object.prototype.pO = 1
  • 7. A ! a = 2 var A = function() { this.a = 2; } Object ! Prototipo de Object ! pO = 1
  • 8. A ! a = 2 Prototipo de A ! Object ! Prototipo de Object ! pO = 1
  • 9. A ! a = 2 Prototipo de A ! A.prototype.pA = 3 Object ! Prototipo de Object ! pO = 1
  • 10. A ! a = 2 Prototipo de A ! pA = 3 A.prototype.pA = 3 Object ! Prototipo de Object ! pO = 1
  • 11. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 var B = function() { this.b = 4; } B ! b = 4
  • 12. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 Prototipo de B !
  • 13. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 Prototipo de B ! B.prototype = new A
  • 14. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A
  • 15. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A
  • 16. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A new A() ! a = 2
  • 17. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A new A() ! a = 2
  • 18. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 B.prototype.pB = 5
  • 19. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 B.prototype.pB = 5
  • 20. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5
  • 21. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B()
  • 22. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B() x new B() ! b = 4
  • 23. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B() x new B() ! b = 4 Prototipo de B !
  • 24. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 25. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.b x new B() ! b = 4 Prototipo de B ! ?
  • 26. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 27. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pB x new B() ! b = 4 Prototipo de B ! ?
  • 28. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 29. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.a x new B() ! b = 4 Prototipo de B ! ?
  • 30. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 31. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pA x new B() ! b = 4 Prototipo de B ! ?
  • 32. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 33. A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pO x new B() ! b = 4 Prototipo de B ! ?
  • 34.
  • 35. function f (x) { return function (y) { return x + y; }; } var a = f(5); var b = a(3); alert(b); // ????
  • 36. Javascript Alexandre Gomes princípios da programação funcional com
  • 37. programação imperativa 1. Bata bem todos os ingredientes ( menos o fermento) da massa no liquidificador, aproximadamente 2 a 3 minutos 2. Acrescente o fermento e bata por mais uns 15 segundos 3. Coloque em uma fôrma redonda, u n t a d a c o m m a n t e i g a e polvilhada com farinha de trigo 4. Asse por cerca de 40 minutos em forno médio (180º graus), preaquecido programação declarativa Comunique assim que estiver pronto. Não deixe o bolo queimar. Não deixe que abram o forno antes da hora.
  • 38. Buscar alunos com menos de 30 anos. alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ]
  • 39. menores = [] ! for (int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ]
  • 40. menores = [] ! for (int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] imperativa
  • 41. alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] Nome Idade 1 Rodrigo 30 2 Patricia 20 3 Marcos 33 4 Ricardo 28
  • 42. Nome Idade 1 Rodrigo 30 2 Patricia 20 3 Marcos 33 4 Ricardo 28 SELECT * FROM alunos WHERE idade < 30
  • 43. Nome Idade 1 Rodrigo 30 2 Patricia 20 3 Marcos 33 4 Ricardo 28 SELECT * FROM alunos WHERE idade < 30 declarativa
  • 44. menores = [] ! for (int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] SELECT * FROM alunos WHERE idade < 30
  • 45.
  • 46. var numbers = [1,2,3,4,5]
  • 47. var numbers = [1,2,3,4,5] var doubled = [] ! for(var i = 0; i < numbers.length; i++) { var newNumber = numbers[i] * 2 doubled.push(newNumber) } console.log(doubled) //=> [2,4,6,8,10]
  • 48. var numbers = [1,2,3,4,5] var doubled = [] ! for(var i = 0; i < numbers.length; i++) { var newNumber = numbers[i] * 2 doubled.push(newNumber) } console.log(doubled) //=> [2,4,6,8,10] var numbers = [1,2,3,4,5] var doubled = numbers.map(function(n) { return n * 2 }) console.log(doubled) //=> [2,4,6,8,10]
  • 49. var numbers = [1,2,3,4,5]! ! ! ! ! !
  • 50. var numbers = [1,2,3,4,5]! var total = 0! ! for(var i = 0; i < numbers.length; i++) {! total += numbers[i]! }! console.log(total) //=> 15
  • 51. var numbers = [1,2,3,4,5]! var total = 0! ! for(var i = 0; i < numbers.length; i++) {! total += numbers[i]! }! console.log(total) //=> 15 var numbers = [1,2,3,4,5]! ! var total = numbers.reduce(function(sum,n){! return sum + n! });! console.log(total) //=> 15
  • 53. SELECT * FROM alunos WHERE idade < 30
  • 54. SELECT * FROM alunos WHERE idade < 30 var doubled = [1,2,3].map(function(n) { return n * 2 })
  • 55. SELECT * FROM alunos WHERE idade < 30 var doubled = [1,2,3].map(function(n) { return n * 2 }) var total = [1,2,3].reduce(function(sum,n){! return sum + n! });
  • 56.
  • 57. function f (x) { return function (y) { return x + y; }; } var a = f(5); var b = a(3); alert(b); // ????
  • 59.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76. a closure (...) is a function or reference to a function together with a referencing environment “ http://en.wikipedia.org/wiki/Closure_(computer_science)
  • 77.
  • 78.
  • 79.
  • 81.
  • 82.
  • 83.
  • 84.
  • 86.
  • 87.
  • 88.
  • 90.
  • 91.
  • 92. a closure (...) is a function or reference to a function together with a referencing environment “
  • 93. closure = function + environment
  • 94. closure = function + environment
  • 95.
  • 96.
  • 97. (
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107. = = =
  • 108. )
  • 109.
  • 110. closure = function + environment
  • 111.
  • 112.
  • 113.
  • 114. ...
  • 115. E o Kiko ?!
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 123.
  • 124. >> “Alexandre Gomes” >> undefined >> undefined
  • 125.