Skip to content

Instantly share code, notes, and snippets.

Avatar
🐺
Woof

Juan Cortés Ross juan-cortes

🐺
Woof
View GitHub Profile
View keybase.md

Keybase proof

I hereby claim:

  • I am juan-cortes on github.
  • I am user8528 (https://keybase.io/user8528) on keybase.
  • I have a public key ASBBXcGP6RSvLqXU26glD4PwfDWka7GmtYlM0FxvBI_2Igo

To claim this, I am signing this object:

View example0.js
function count() {
var counter = 0;
return function innerCount() {
return ++counter;
}
}
var counter1 = count(); //Create a new counter
var counter2 = count(); //Create a new counter
counter1(); //returns 1
@juan-cortes
juan-cortes / example3.js
Created Jun 28, 2018
Extended example of closures in javascript, adding memoization to the mix
View example3.js
function powerOfNFactory(power){
var counter = 0;
var cache = {};
return function generatedFunction(subject){
counter++;
console.log(`Dyanmic Power of ${power} called ${counter} time/s`);
if (cache[subject] === undefined) {
cache[subject] = Math.pow(subject, power);
} else {
console.log("Cached!");
@juan-cortes
juan-cortes / example2.js
Last active Jun 28, 2018
Expanded example of closures in javascript, adding a counter
View example2.js
function powerOfNFactory(power){
var counter = 0
return function generatedFunction(subject){
counter++;
console.log(`Dyanmic Power of ${power} called ${counter} time/s`);
return Math.pow(subject, power);
}
}
var powerOf2 = powerOfNFactory(2);
@juan-cortes
juan-cortes / example.js
Last active Jun 28, 2018
Closure example function factory
View example.js
function powerOfNFactory(power){
return function generatedFunction(subject){
return Math.pow(subject, power);
}
}