space, → | next slide |
← | previous slide |
d | debug mode |
## <ret> | go to slide # |
c | table of contents (vi) |
f | toggle footer |
r | reload slides |
z | toggle help (this) |
var a = 1, //Number b = 2.0e10, //Number c = false, //Boolean d = 'hola', //String e = "mundo" //String f = new Date(),//Date g = /[a-z]+/ //RegExp h, //undefined (nulidad implícita) i = null //nulidad explícita
var
var
la hace ¡global!a = b + c (d + e).toString() x = [1,2,3] [4,5,6].concat([7,8,9])
a = b + c(d+e).toString() x = [1,2,3][4,5,6].concat([7,8,9])
a = b + c; (d + e).toString() x = [1,2,3]; [4,5,6].concat([7,8,9])
x = 0 || "" || 666 //x es 666 y = 0 && 1/0 || "uff" Boolean(y) // true
Boolean(algo)
var arr = "hola mundo adiós hola".split(" "), saludos = 0 for(var i=0; i<arr.length; i++) switch(arr[i]){ case "hola": saludos++ } if(saludos > 1) console.log("¡Me saludaron!") while(saludos){ console.log("Y también hay do...while") saludos-- }
var a = [1,2,3,4], b = [1, true, 2, 3, ""] a.concat(b) b.slice(1,3).join() //"true,2" "esto es un string".split(" ").sort()[2] //[ 'es', 'esto', 'string', 'un' ] //esto
var a = [1,2,3] a.pop() //3, [1,2] a.push(4) //[1,2,4] a.shift() //[2,4]
var a = {foo: 1, bar: 2, 'baz': 6} a.foo a['bar'] typeof a a.otro = 3 a['otro'] for(var p in a) a[p] = p a
function suma(a,b){ return a+b } suma(1,2) var resta = function(x,y){ return x-y } resta(2,1)
function meVale(a,b){ x = a || b || "no me mandaste nada :(" return x; } meVale(1,2) meVale(1) meVale(null, 2) meVale()
function sum(){ console.log(arguments.callee.name) console.log(typeof arguments) var res = 0; for(var i = 0; i < arguments.length; i++) res += arguments[i] return res } sum(1,2,3,4)
typeof(function(){}) var x = { metodo: function(y){return y*y} } x.metodo(2) x.otro = function(n){ return n*n*n; } x.otro(3) x.otro.apply(null, [3]) x.otro.name //es anónima! x.conNombre = function hola(){} x['conNombre'].name
Array
tiene algunas operaciones de iteración interesantesvar squares = [1,2,3,4].map(function(n){ return n*n; }) var odds = [4,3,2,1].filter(function(n){ return n%2 })
function negate(f){ return function(){ return !f.apply(null, arguments) } } function verdad(){ return true; } mentira = negate(verdad) mentira()
var Response = function(body, status, headers){ this.body = typeof body == 'object' ? JSON.stringify(body): body this.status = status || 200 this.headers = headers || {'Content-Type': "text/plain"} this.to_a = function(){ return [this.body, this.status, this.headers] } } var r = new Response({hola: "mundo"}) r.to_a()
new
, todo lo que esté en this
se pondrá dentro de un nuevo objetoResponse.prototype.getBody = function(){ return this.body } r.getBody()
Object.prototype.merge = function(other){ for(var prop in other){ //por el prototipo if(other.hasOwnProperty(prop)) this[prop] = other[prop] } }