Skip to content

Instantly share code, notes, and snippets.

@Lenninlasd
Last active February 7, 2019 01:46
Show Gist options
  • Save Lenninlasd/0426cc46527a8f573c8b2c1c58e0c681 to your computer and use it in GitHub Desktop.
Save Lenninlasd/0426cc46527a8f573c8b2c1c58e0c681 to your computer and use it in GitHub Desktop.
/*
==========================================
Prototypal syntax vs Class syntax
==========================================
*/
function Poligono(alto, ancho) {
this.alto = alto;
this.ancho = ancho;
}
Poligono.prototype.calcArea = function() {
return this.alto * this.ancho;
}
var cuadrado = new Poligono(10, 10);
cuadrado.calcArea() // 100
// --------------- Now -----------------
class Poligono {
constructor (alto, ancho) {
this.alto = alto;
this.ancho = ancho;
}
calcArea () {
return this.alto * this.ancho ;
}
}
const cuadrado = new Poligono(10, 10);
cuadrado.calcArea(); // 100
/*
==========================================
Spread parameters
==========================================
*/
var list = [10, 20];
function calcArea( alto, ancho){
return alto * ancho;
}
calcArea.apply(null, list); // 200
// --------------- Now -----------------
calcArea(...list); // 200
/*
==========================================
Default parameters
==========================================
*/
function calcArea( alto, ancho){
alto = (typeof alto !== 'undefined') ? alto : 10;
ancho = (typeof ancho !== 'undefined') ? ancho : 10;
return alto * ancho;
}
// --------------- Now -----------------
function calcArea( alto=10, ancho=10){
return alto * ancho;
}
/*
==========================================
`let` hoisting
==========================================
*/
for (var i = 1; i <= 5; i++){
(function(i){
setTimeout(function() {
console.log(i);
}, 100*i);
})(i);
}
// --------------- Now -----------------
for (let i = 1; i <= 5; i++){
setTimeout(
() => console.log(i),
100*i
);
}
/*
==========================================
fetch vs XMLHttpRequest
==========================================
*/
var req = new XMLHttpRequest();
req.open(
'GET',
'https://barranquillajs.com/manifest.json',
true
);
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200)
console.log(req.responseText);
else
console.log("Error loading page\n");
}
};
req.send(null);
// --------------- Now -----------------
fetch('https://barranquillajs.com/manifest.json')
.then(res => res.json())
.then(console.log);
// =====================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment