Skip to content

Instantly share code, notes, and snippets.

@wila-diaz
Created January 6, 2021 15:12
Show Gist options
  • Save wila-diaz/ccd755db5d0b9d1e60f1d59d176736c4 to your computer and use it in GitHub Desktop.
Save wila-diaz/ccd755db5d0b9d1e60f1d59d176736c4 to your computer and use it in GitHub Desktop.
Example Scope in Js
function operacionescon(number){
let first_number = number
return {
suma: function (other_number){
return first_number + other_number
},
resta:function(other_number){
return first_number - other_number
},
multiplica: function (other_number){
return first_number * other_number
},
divide:function (other_number){
return first_number / other_number
}
}
}
let operocon2 = operacionescon(2)
console.log(operocon2.suma(3))
console.log(operocon2.resta(3))
console.log(operocon2.multiplica(3))
console.log(operocon2.divide(3))
// Scope global
let var1 = 'Hola';
const var2 = 'Hi';
// function Scope
function scope(number) {
if (number >= 3) {
// block scope
//Re-escribir la variable var1 porque se definió con let
var1='La multiplicación da'
let number_two = 100;
return console.log(`${var1} ${number * number_two}`);
} else {
// block scope
// No se puede re-escribir var2, porque se definió con const
//var2='la suma es ahora'
let number_three = 200;
return console.log(`${var2} ${number + number_three}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment