Published
Edited
Nov 25, 2019
2 forks
1 star
Insert cell
md`# Advanced JS
**Today topics**:
1. setInterval & setTimeout
2. Context & Function invocation
3. JS Scope & Closure

Learnings:
* JS | Async & Callbacks v1
* JS | Context & Function invocation (bonus)
* JS | Closures & Scope
* JS | Debugging, Error Handling and JS Hint

`
Insert cell
md`## 1. setInterval & setTimeout`
Insert cell
/*{
console.log("Hola")
setTimeout(function(){
console.log("Adios")
},3000);
console.log("Hola2")
}*/
Insert cell
{
/*console.log("Hola")
setInterval(function(){
console.log("Adios")
},3000);
console.log("Hola2")*/
}
Insert cell
{
/*setInterval(function(){
console.log("Pepe")
},0);
setInterval(function(){
console.log("Pepe2")
},0);
*/
console.log("IntervalID is:")
}
Insert cell
md`## 2. Context & Function invocation`
Insert cell
md`### 2.1 Global context vs local context - (this)`
Insert cell
function suma(a,b){
console.log(this)
return a+b;
}
Insert cell
suma(5,4)
Insert cell
mando = {
return {
pilas:0,
addPila: function(){
console.log(this)
this.pilas++;
}
}
}
Insert cell
{
// Esto da un error porque la invocación no es como método sino como funcion.
let x = mando.addPila
x();
}
Insert cell
mando2 = {
return {
pilas:0,
addPila: function(){
var that = this;
function test(a){
console.log(that);
console.log("Prueba",a);
that.pilas++;
}
return test;
}
}
}
Insert cell
mando2.addPila()("X")
Insert cell
md`### 2.2 Types of invocations: method, function, constructor`
Insert cell
{
var value = 500;
var obj = {
value: 0,
increment: function() {
var that = this;
that.value++;

var innerFunction = function() {
console.log(that.value);
}

innerFunction(); // Function invocation pattern
}
}
obj.increment();
}
Insert cell
md`### 2.3 call, apply, bind`
// https://repl.it/@boyander/bind
// https://repl.it/@boyander/Context-call-apply
Insert cell
md`## 3. JS Scope & Closure`
Insert cell
md`### 3.1 Global vs local Scope`
Insert cell
{

var fn = function(){
var z = 10;
var fn2 = function(){
console.log("La variable del clousure Z es:",z);
}
return fn2;
}

var q = fn()
q()

}
Insert cell
md`### 3.2 Closure`
Insert cell
{

var z = 1000;
function hola(){
console.log("Z vale:",z);
}
hola();
console.log(z)
}
Insert cell
{

function clExample(){
var a = 50;
console.log(a)
}
clExample();
console.log(a);
}
Insert cell
{
for(var i = 0; i < 10; i++){
var q = 5
console.log(i)
}
console.log(q);
console.log("ultimo valor",i)
}
Insert cell
{
let i = 0;
for(i = 0; i < 10; i++){
console.log(i)
}
console.log("ultimo valor",i)
}
Insert cell
md`**clousure definition:** functions that refer to independent (free) variables. In other words, the function defined in the closure ‘remembers’ the environment in which it was created.`
Insert cell
md`### 3.3 Hoisting, Shadowing`
Insert cell
{
// Shadowing
var j = 10;
function hola(){
var j = 100;
console.log("Dentro",j);
}
hola();
console.log("Fuera",j)
}
Insert cell
{
// Shadowing con hoisting
var z = 10;
function saludo(){
console.log(z);
var z = 50;
}
saludo();
console.log(z);
}
Insert cell
{
// VARIABLE HOISTING
console.log(m)
var m = 5;
// var m;
// console.log(m);
// m = 5;

}
Insert cell
{
// FUNCTION HOISTING
saludo();
function saludo(){
console.log("Hola");
}
}
Insert cell
{
m();
let m = function (){
console.log("pepe")
}
}
Insert cell
{
var name = "Fer";

function changeName() {
console.log("Original name was " + name);

var name = "Harry";
console.log("New name is " + name);
}

changeName();

}
Insert cell
md`### 3.4 IIFE - Immediately-invoked function expression`
Insert cell
(function(){
console.log("JUAN");
})()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more