Skip to content

Instantly share code, notes, and snippets.

@eiximenis
Created November 27, 2014 10:04
Show Gist options
  • Save eiximenis/12d92b47d7715302fd8e to your computer and use it in GitHub Desktop.
Save eiximenis/12d92b47d7715302fd8e to your computer and use it in GitHub Desktop.
Ejemplos de la charla Una tapa de EcmaScript. PPT http://www.slideshare.net/eduardtomas/una-tapa-de-ecmascript-6
(function() {
const a=10;
a=20;
let b=10;
b=20;
console.log("a-->" + a);
console.log("b-->" + b);
var s = true;
while (s) {
const ic = 10;
var gic = 10;
s = false;
}
console.log("gic-->" + gic);
console.log("ic-->" + ic);
})();
(function() {
for (let i=0; i<2; i++) {
console.log("i-->" + i);
}
for (var gi=0; gi<2; gi++) {
console.log("gi-->" + gi);
}
console.log("i-->" + i);
console.log("gi-->" + gi);
})();
(function() {
var [a, b, c] = [10, 20, 30];
console.log("a-->", a);
console.log("b-->", b);
console.log("c-->", c);
var [,d,,e] = [10,20, 30, 40];
console.log("d-->", d);
console.log("e-->", e);
function fibo() {
return [1,1,2,3,5,8];
}
var [,f,,g] = fibo();
console.log("f-->", f);
console.log("g-->", g);
var {m: month, y: year } = {m: 10, y: 20};
console.log("month-->", month);
function now() {
return {y:2014, m:12, d:26};
}
var {m: fmonth, y: fyear } = now();
console.log("fmonth-->", fmonth);
})();
(function() {
function foo(a,b) {
console.log("foo.a-->",a);
console.log("foo.b->",b);
console.log("foo.args-->", arguments.length);
}
function optfoo(a, b=10) {
console.log("optfoo.a-->",a);
console.log("optfoo.b->",b);
console.log("optfoo.args-->", arguments.length);
}
foo(5);
optfoo(5);
})();
(function() {
function variadic(a,b,...v) {
console.log("a-->", a);
console.log("b-->", b);
console.log("v-->", v);
console.log("----");
}
variadic(1,2);
variadic(1,2,3);
variadic(1,2,3,4,5,6,7);
})();
(function() {
var a = [1,2,3,4];
function foo(x, y, ...z) {
console.log("x-->", x);
console.log("y-->", y);
console.log("z-->", z);
}
foo(10, 20, a);
foo (10, 20,...a);
})();
(function() {
let [a, b, ...c] = [10, 20, 30, 40, 50];
console.log(c);
})();
(function() {
var a = [1,2,3];
var b = [4,5,6];
// Modo ES5 de añadir b a a:
Array.prototype.push.apply(a,b);
console.log(a);
var a2= [1,2,3];
a2.push(...b);
console.log(a2);
})();
(function(){
var Animal = function() {
this.age = 10;
this.grow = () => {
setInterval( () => console.log(++this.age), 1000);
};
}
var dog = new Animal();
dog.age = 10;
dog.grow();
})();
(function() {
var url = 'http://www.google.com';
let one = 1;
function get_one() {
return 1;
}
var data = {url, method: 'POST', one: get_one()};
for (let k in data) {
console.log(k, data[k]);
}
});
(function() {
function arrayIterator() {
var idx = 0;
return {
next: () => {
return idx < this.length ?
{done: false, value: this[idx++]} :
{done: true};
}
}
}
Array.prototype.makeIterator = arrayIterator;
var iterator = [1,2,3,4].makeIterator();
console.log(iterator.next().value);
console.log(iterator.next().value);
})();
(function() {
var Beers = function() {
this.data = ["estrella", "voll damm", "guinness"];
this.iterator = () => {
var idx = 0;
return {
next: () => {
return idx < this.data.length ?
{done: false, value: this.data[idx++]} :
{done: true};
}
}
};
this['@@iterator'] = this.iterator;
}
var mb = new Beers();
for (let beer of mb) {
console.log(beer);
}
})();
(function() {
var Beers = function*() {
yield "Estrella";
yield "Voll Damm";
yield "Guinness";
yield "Sang de gossa";
}
var beers = Beers();
for (let beer of beers) {
console.log(beer);
}
// 1. Es un iterador
console.log("beers.next is ",typeof(beers.next));
// 2. es un iterable
console.log("beers @@iterator is ", typeof(beers['@@iterator']));
// 3. Podemos usar operator para pasar todas las cervezas a un array
var allbeers = [...Beers()];
console.log(allbeers);
// 4. Generadores se leen una vez
var allbeersagain = [...beers];
var allbeersnew = [...Beers()];
console.log(allbeersagain.length, allbeersnew.length);
})();
(function() {
var fibo = function*() {
let [prev, curr] = [0, 1];
while(true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
};
var seq = fibo();
console.log(seq.next().value);
console.log(seq.next().value);
console.log(seq.next().value);
console.log(seq.next().value);
})();
(function() {
var m = new Map();
m.set(5, "five");
m.set("5", "five as string");
m.set(2, {value: 2});
console.log(m.has(2));
console.log(m.has("2"));
m.forEach((k,v) => console.log("Key is ", k, " and value is ", v));
for (let kv of m) {
console.log(kv);
}
})();
(function() {
class Animal {
constructor (name) {
this.name = name;
}
toObject() {
return {name: this.name};
}
}
var dog = new Animal('Puppy');
console.log(dog.toObjec());
})();
(function() {
var Beer = function(name, ibus) {
this.name = name;
if (ibus) {
this.ibus = ibus;
}
}
var bhandler = {
get: function (target, name) {
return name in target ? target[name] : 42;
},
set: function(target, name, value) {
if (name == "ibus" && value < 100) {
target[name] = value;
}
}
}
var pbeer= new Proxy(new Beer('estrella', 15), bhandler)
var pbeer2 = new Proxy(new Beer('volldamm'), bhandler)
console.log(pbeer.name, pbeer.ibus);
console.log(pbeer2.name, pbeer2.ibus);
pbeer2.ibus = 1000;
console.log(pbeer2.ibus);
pbeer2.ibus=10;
console.log(pbeer2.ibus);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment