Más contenido relacionado La actualidad más candente (8) Similar a Mejorando.la: Curso Profesional de Frontend, Dominando JavaScript (20) Mejorando.la: Curso Profesional de Frontend, Dominando JavaScript 3. acerca de mi
• Ingeniero de Sistemas…
• Cofundador de monoku y monomi.co
• Co-organizador de BogotáJS
• Co-organizador de JSConf.co
5. Agenda
• JavaScript: Las partes buenas
• JavaScript Orientado a Objetos (OO)
• Closures, Mixins, Objetos, Prototipos
• Patrones de Diseño y MV*
• APIs Rest, AJAX y Sockets (*)
9. JavaScript: The Good Parts
80% Intro a JS
2% Hermosas características
10% Feas características
10% Malas partes
12. var a = 5, b = 10
!
function noGlobal( a ){
var b = 3
a = a*2
return a * b
}
!
noGlobal( 1 ) // 6
a // 5
b // 3
21. '' == '0' // false
0 == '' // true
0 == '0' // true
!
false == 'false' // false
false == '0' // true
!
false == undefined // false
false == null // false
null == undefined // true
!
' trn ' == 0 // true
22. '' === '0' // false
0 === '' // false
0 === '0' // false
!
false === 'false' // false
false === '0' // false
!
false === undefined // false
false === null // false
null === undefined // false
!
' trn ' === 0 // false
26. var a = { p: 0 }, c
!
with( a ){
p = 3
c = 5
}
!
a.p // 3
a.c // undefined
c // 5
32. No usen eval para ejecutar código desde el
servidor u otro servicio AJAX
34. ... fall through ..
!
va de case en case naturalmente
(requiere usar break)
valida con equivalencia simple
35. var blah = 0;
switch (blah) {
case -1:
alert('negative 1');
case 0:
alert(0)
case 1:
alert(1);
break;
case 2:
alert(2);
break;
default:
alert('default');
}
37. Punto y Coma Opcional
(optional semicolon)
!
- Código más limpio -
45. var a = [1,2,3,4], i = 0
a[i++] // 1
a[++i] // 3
50. el orden de definición de variables y
funciones afecta el resultado
56. var a = 10
!
function f(){
alert( a )
var a = 5
}
!
f()
57. var a = 10
!
function f(){
alert( a )
var a = 5
}
!
f() // undefined
58. var a = 10
!
function f(){
var a
alert( a )
a = 5
}
!
f() // undefined
59. var a = 10
!
function f(){
a = 5
alert( a )
function a(){}
}
!
f() // ?
a // ?
60. var a = 10
function f(){
a = 5
alert( a )
function a(){}
}
f() // 5
a // ?
61. var a = 10
function f(){
a = 5
alert( a )
function a(){}
}
f() // 5
a // 5 ? no... 10!!
66. function Persona( name ){
this.name = name
}
Persona.prototype.alertName = function(){
alert( this.name )
}
Persona.prototype.setAge = function( age ){
this.age = age
}
var juan = new Persona( 'juan' )
74. ¿first-class objects?
• se pueden asignar a variables
• se puede recibir/pasar como
argumentos
• se pueden retornar en una función
• son objetos y por lo tanto tienen
propiedades
77. var o = {
i: 0,
f: function(){
++ this.i
return this
}
}
o.f().f().f()
o.i // 3
80. Es como una sala de espera en
la que dependiendo del turno
tu puedes ser llamado
81. function caller( cb ){
setTimeout(function(){
cb( new Date() )
}, 1000)
}
caller(function( now ){
alert( now )
})