Closures
How do I make an closures?
What is a closures? How do you make a closures? This script and codes were developed by Dave on 17 September 2022, Saturday.
Closures - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Closures</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
</head>
<body> <button onClick="funcStorer()">FuncStorer</button>
<button onClick="funcStorer2()">FuncStorer2</button> <script src="js/index.js"></script>
</body>
</html>
Closures - Script Codes JS Codes
/* EXAMPLE ONE */
function closureIncrementer() { let x = 0; function incrementer() { console.log(++x); } return incrementer;
}
/* EXAMPLE ONE B */
// function closureIncrementer() {
// let x = 0;
// return function() { // console.log(++x); // }
// }
/* EXAMPLE ONE C */
// const closureIncrementer = () => {
// let x = 1;
// const incrementer = () => console.log(++x);
// return incrementer;
// }
// const funcStorer = closureIncrementer();
// const funcStorer2 = closureIncrementer();
// funcStorer(); // returns 1 - calls closureIncrementer
// funcStorer(); // returns 2 - calls incrementer
// funcStorer2(); // returns 1 - calls closureIncrementer
// funcStorer(); // returns 3 - calls incrementer
/* EXAMPLE ONE D */
//const closureIncrementer = (x = 1) => () => console.log(x++);
const funcStorer = closureIncrementer();
const funcStorer2 = closureIncrementer();
// /* EXAMPLE TWO */
// function add(num){
// var num1 = num;
// function addToNum1(num2){
// return num1 + num2
// };
// return addToNum1;
// };
// var add5 = add(5)
// add5(2); // 7
// add5(5); // 10
// add5 = add(10);
// add5(2); // 12
// /* EXAMPLE THREE */
// function counter() {
// var n = 0;
// return {
// count: function() { return ++n; },
// reset: function() { n = 0; }
// };
// };
// var myCounter = counter();
// myCounter.count();
// /* EXAMPLE FOUR */
// function sayAlice(){
// function makeLog() {
// console.log(alice);
// };
// var alice = 'Why hello there, Alice';
// return makeLog;
// };
// var whatDoesSheSay = sayAlice();
// whatDoesSheSay(); // 'why hello there Alice'
// /* EXAMPLE FIVE */
// function makeStopWatch(){
// console.log('initialised');
// var elapsed = 0;
// console.log(elapsed);
// function stopwatch() {
// console.log('stopwatch');
// return elapsed;
// }
// function increase(){ elapsed++ };
// setInterval(increase, 1000);
// return stopwatch;
// }
// var watch = makeStopWatch();
// watch();
// var otherWatch = makeStopWatch();
// otherWatch();
/* RECIPE
1. Create your parent function
2. define some variables in parents local scope
3. define a function inside the parent(child funciont)
4. return that function from inside the parent
EXECUTION
1. Run parent function and save to var. This will hold whatever that function returns
2. optional check what it holds ( should be inner function)
3. Run the inner function
4. what happens?
*/
Developer | Dave |
Username | DaveOrDead |
Uploaded | September 17, 2022 |
Rating | 3 |
Size | 2,218 Kb |
Views | 22,264 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
React dropdown | 8,538 Kb |
React Calculator | 30,708 Kb |
Shell CSS lib | 43,289 Kb |
Character Gen - React | 37,586 Kb |
Flexbox grid mark 2 | 2,131 Kb |
React Tic Tac Toe | 28,062 Kb |
Flexbox Grid - equal height | 2,855 Kb |
Toggle component | 29,331 Kb |
Words at War | 32,346 Kb |
Dice roller | 4,109 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Sticky notes with CSS3 | HaiNguyen007 | 2,146 Kb |
CSS Variables | Jdsteinbach | 4,759 Kb |
Polygon Dodecahedron in CSS | Vennsoh | 7,606 Kb |
Clock Face Idea | Chrisburnell | 3,196 Kb |
Highbrow Basic HTML Lesson 7 | Kimlarocca | 1,662 Kb |
Perspective Origin Demo | Agelber | 3,614 Kb |
STAR WARS LIGHTSABER | Francoiscoron | 4,420 Kb |
Particle Motion trajectories | Sniejadlik | 5,899 Kb |
Gears | Synvox | 3,278 Kb |
Fluid Responsive Typography | Jonmilner | 4,205 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!