Closures

Developer
Size
2,218 Kb
Views
22,264

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 Previews

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?
*/
Closures - Script Codes
Closures - Script Codes
Home Page Home
Developer Dave
Username DaveOrDead
Uploaded September 17, 2022
Rating 3
Size 2,218 Kb
Views 22,264
Do you need developer help for Closures?

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!

Dave (DaveOrDead) Script Codes
Create amazing video scripts with AI!

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!