Arcadio García Salvadores
Universidad Autónoma de Madrid
@arcadio_g_s
#MSPSpain
//Las funciones son de primera clase
var suma = function (a, b) {
return a + b;
}
//Y también son de orden superior
function aplicaTres(f,x) {
return f(3,x);
}
aplicaTres(suma,1); //4
var data = [{ user: 'pedro', msg: 'hola' }, { user: 'marta', msg:
'hola' }, { user: 'pedro', msg: 'adios' }, { user: 'admin',
msg: 'test' }];
var mensajes = data.map(function (x) { return x.msg; });
var mensajesFormat = data.map(function (x) {
return x.user + ' dice: ' + x.msg;
});
var data = [{user:'pedro',msg:'hola'}, {user:'marta',msg:'hola'},
{user:'pedro',msg:'adios'}, {user:'admin',msg:'test'}];
var mensajesPedro = data.filter( function (x) {
return x.user == 'pedro';
});
var data = [{user:'pedro',msg:'hola'}, {user:'marta',msg:'hola'},{user:'pedro',msg:'adios'},
{user:'admin',msg:'test'}];
var numeroMensajes = data.reduce( function (previousValue, currentValue, index, array) {
previousValue[currentValue.user]=(previousValue[currentValue.user] || 0) + 1;
return previousValue;
}, {});
function curry (fn, scope) {
var scope = scope || window;
var args = [];
for (var i=2, len = arguments.length; i < len; ++i) {
args.push(arguments[i]);
};
return function() {
fn.apply(scope, );
};
}
var add3=curry(sumar, this, 3);
add3(4); //7
var a = 0, b = 0;
function foo() {
var a = 1, b = 1;
if (true) {
var a = 2; //Redeclaracion
a = 2;
let b = 2; //b=2
}
//a=2, b=1
}
//a=0,b=0
var a=0;
let b=0;
const c=0;
const d=[1,2,3];
const e={x:3,y:2};
a=3;
b=3;
c=3; //ReferenceError
d.push(4);
d=[];//ReferenceError
e.z=4;
const d=[1,2,3];
const e={x:3,y:2};
e.z=3; //Se puede hacer
Object.seal(e); //No permite añadir mas valores
Object.freeze(e); //No permite modificar valores
var a = [1, 2, 3];
function sum(x, y, z) {
return x + y + z;
}
sum(...a); //6
function sum(...z) {
return z.reduce(function (previous, current) {
return previous + current;
});
}
sum(1, 2, 3, 4, 5); //15
function creaRobot (esAsesino){
...
}
creaRobot() ???
function creaRobot (esAsesino){
var asesino = esAsesino || false;
var asesino = (esAsesino !== undefined) ? esAsesino : false;
...
}
function creaRobot (esAsesino=false){
...
}
var a = [1,2,3];
var b = {x:1,y:2,z:3};
var [ax, ay, az] = a;
//ax==1, ay==2, az==3
var {bx, by, bz}=b;
//bx==1, by==2, bz==3
var {bx, by, bz=0}=b;
[1,2,3].map(function(x){ return x*x});
[1.2.3].map(x=>x*x);
NO altera this
var numero={
duplica: ()=> this*this;
}
numero.duplica(); //falla
var coordenadas = { x: 1, y: 2, z: 3 };
for (var i in coordenadas) {
console.log(i); //x,y,z
console.log(coordenadas[i]); //1,2,3
}
for(var i of coordenadas) {
console.log(i); //1,2,3
}
//matematicas.js
export function cuadrado(x){
return x*x
}
//main.js
import cuadrado from “matematicas.js”
import * as matematicas from “matematicas.js”
cuadrado(3);
matematicas.cuadrado(3);
class Vector{
constructor(x,y){
this.x=x;
thix.y=y;
}
modulo(){
return Math.sqrt(this.x*this.x+this.y*this.y);
}
}
var v1 = new Vector(1,1);
class NumeroComplejo extends Vector{
parteReal(){
return this.x;
}
parteImaginaria(){
return this.y;
}
}
var z1 = new NumeroComplejo(1,1);
let map = new Map();
map.set('foo', 123);
map.get('foo');
map.has(“foo”);
map.delete('foo');
map.has(“foo”);
map.forEach((value,key)=>console.log(value,key));
let set = new Set(); //o WeakSet
set.add('foo');
set.size();
set.has(“foo”);
set.delete('foo');
set.has(“foo”);
set.forEach((value,key)=>console.log(value,key));
var new
u32[2]=3;
u32[0]=“a”;
u32[20]=4;
https://github.com/getify/You-Dont-Know-JS
http://exploringjs.com
Youtube: @mpjm, @jsconf
dev.modern.ie, MDN
http://caniuse.com
https://github.com/uamnet/techtalks

Functional JS+ ES6.pptx

Editor's Notes

  • #10 De los estandarizadores del disquete y C# Completa retrocomp 245 a 600 pag
  • #11 De los estandarizadores del disquete y C# Completa retrocomp 245 a 600 pag