Last active
July 22, 2019 07:55
-
-
Save erikfenriz/786be597b6530f1d0b908161ba9f0549 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function largestOfFour(arr) { | |
let maxOfArray = []; | |
return function iterator(arr){ | |
for (var i = 0; i < arr.length; i++) | |
if (Array.isArray(arr[i])){ | |
console.log(arr[i]); | |
iterator(arr[i]) | |
} | |
else{ | |
console.log(arr[i]) | |
} | |
} | |
} | |
largestOfFour()([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); | |
console.log('--------------------------------'); | |
const array = [1,2,3,4]; | |
for(var i = 0; i < array.length; i++){ | |
(function(closureI){ | |
setTimeout(function(){ | |
console.log("I am at index " + array[closureI]) | |
},1000) | |
})(i) | |
} | |
console.log('--------------------------------'); | |
function count(){ | |
let changedArray = []; | |
return function(array){ | |
for(var i = 0; i < array.length; i++){ | |
changedArray.push(i); | |
console.log(changedArray); | |
setTimeout(function(){ | |
console.log("I am at index " + changedArray[0]) | |
changedArray.shift(); | |
}, 1000) | |
} | |
} | |
} | |
const counting = count(); | |
counting([1,2,3,4]); | |
console.log('--------------------------------'); | |
function initialize(){ | |
let isInitialized = false; | |
let view; | |
return function setView(){ | |
if(isInitialized){ | |
return undefined; | |
} | |
else | |
{ | |
isInitialized = true; | |
return view = "perfect image"; | |
} | |
} | |
} | |
const startOnce = initialize(); | |
startOnce() | |
console.log('--------------------------------'); | |
const makeNuclearButton = () => { | |
let timeWithoutDestruction = 0; | |
const passTime = () => timeWithoutDestruction++; | |
const totalPeaceTime = () => timeWithoutDestruction; | |
const launch = () => { | |
timeWithoutDestruction = -1; | |
return '💥'; | |
} | |
setInterval(passTime, 1000); | |
return {totalPeaceTime} | |
} | |
const ww3 = makeNuclearButton(); | |
ww3.totalPeaceTime() // example of encapsulation | |
console.log('--------------------------------'); | |
var increment = (function(n) { | |
return function() { | |
n += 1; | |
return n; | |
} | |
}(0)); // -1 if you want the first increment to return 0 | |
console.log('--------------------------------'); | |
var increment = function() { | |
var i = 0; | |
return function() { return i += 1; }; | |
}; | |
var ob = increment(); | |
console.log('--------------------------------'); | |
function greet(whattosay) { | |
return function(name) { | |
console.log(whattosay + ' ' + name); | |
} | |
} | |
greet('Hi')('Tri'); | |
var sayHi = greet('Hi'); | |
sayHi('Tri'); | |
console.log('--------------------------------'); | |
// A classic example of closure | |
function buildFunctions() { | |
var arr = []; | |
for (var i = 0; i < 3; i++) { | |
arr.push( | |
function () { | |
console.log(i); // draw out the call stack, you will see how all three | |
// call to console.log(i) will point to the same i | |
// which last value is 3 | |
} | |
) | |
} | |
return arr; | |
} | |
var fs = buildFunctions(); | |
fs[0](); | |
fs[1](); | |
fs[2](); | |
console.log('---------------'); | |
function buildFunctions2() { | |
var arr = []; | |
for (var i = 0; i < 3; i++) { | |
let j = i; | |
arr.push( | |
(function(j) { | |
return function() {console.log(j);} | |
})(i) // Using immediate invoke function expression to create a new execution context | |
// This way i will be freezed in to the execution context | |
); | |
} | |
return arr; | |
} | |
var fs2 = buildFunctions2(); | |
fs2[0](); | |
fs2[1](); | |
fs2[2](); | |
console.log('---------------'); | |
function buildFunctions3() { | |
var arr = []; | |
for (var i = 0; i < 3; i++) { | |
let j = i; | |
arr.push( | |
function () { | |
console.log(j); // using the keyword let in ES6, j is scope to the block, | |
// everytime a new j in memory is created, | |
// console.log(j) will pointing to a diffrent memory space for j | |
// that has the value 0, 1, 2 | |
} | |
) | |
} | |
return arr; | |
} | |
var fs3 = buildFunctions3(); | |
fs3[0](); | |
fs3[1](); | |
fs3[2](); | |
console.log('--------------------------------'); | |
// Another classic case on closure | |
// See the stackoverflow thread for more info | |
// http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example | |
function temp(i) { | |
setTimeout(function() { | |
console.log(i); | |
}, 2000) | |
} | |
// classic Javascript | |
for (var i = 0; i < 3; i++) { | |
temp(i); | |
} | |
for (var i = 0; i < 3; i++) { | |
setTimeout(function() { | |
console.log(i); | |
}, 2000); | |
} | |
// ES6 with let keyword | |
for (let i = 0; i < 3; i++) { | |
temp(i); | |
} | |
for (let i = 0; i < 3; i++) { | |
setTimeout(function() { | |
console.log(i); | |
}, 2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment