4. //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
6. 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';
});
7. 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;
}, {});
8. 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
9.
10.
11. 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
12. 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;
17. function creaRobot (esAsesino){
var asesino = esAsesino || false;
var asesino = (esAsesino !== undefined) ? esAsesino : false;
...
}
function creaRobot (esAsesino=false){
...
}
18. 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;
23. class NumeroComplejo extends Vector{
parteReal(){
return this.x;
}
parteImaginaria(){
return this.y;
}
}
var z1 = new NumeroComplejo(1,1);
24. 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));
25. 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));