Difference between revisions of "JavaScript/C3/Closure-in-JS/English"
Nancyvarkey (Talk | contribs) |
Nancyvarkey (Talk | contribs) |
||
(One intermediate revision by one other user not shown) | |||
Line 3: | Line 3: | ||
Author: Jayesh Katta Ramalingaiah | Author: Jayesh Katta Ramalingaiah | ||
− | Domain Reviewer: | + | Domain Reviewer: Ankita Maske |
− | Novice Reviewer: | + | Novice Reviewer: Praveeen S. |
Keywords: JavaScript, HTML, document, closure, function | Keywords: JavaScript, HTML, document, closure, function | ||
Line 121: | Line 121: | ||
Click on the '''Console''' tab | Click on the '''Console''' tab | ||
− | || Now open the ''' | + | || Now open the '''developer tools''' panel and go to the '''console tab''' |
|- | |- | ||
Line 290: | Line 290: | ||
− | This is because the '''Closure '''node is hidden in '''Firefox | + | This is because the '''Closure '''node is hidden in '''Firefox developer tools'''. |
|- | |- |
Latest revision as of 11:09, 21 June 2021
Title of the script:Closure
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Ankita Maske
Novice Reviewer: Praveeen S.
Keywords: JavaScript, HTML, document, closure, function
|
|
Slide: Title | Hello and Welcome to the spoken tutorial on “Closure in JS”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However you may use any other browser of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Closure | A closure is a function having access to the parent scope, even after the parent function has closed. |
Only Narration | Let us take an example and understand this better. |
Show VS editor | Open Visual Studio Code editor |
[Editor] Welcome Page -> Open Folder -> Practice-JS | In the editor, browse and open the folder “Practice-JS ”. |
[Editor] Click on
|
Under Practice-JS folder, open the file named index.html
|
[Editor] Type:
<html lang="en"> <head> <title>Closure</title> </head> <body> <script src = "main.js" > </script> </body> </html> |
In the index.html file, update the code as shown.
|
Press: Ctrl + S | Save the file. |
Press: Alt + L and Alt + O | Start the Live server. |
Show firefox | The default browser will open automatically and a new tab opens. |
[Firefox] Press Ctrl + Shift + I
|
Now open the developer tools panel and go to the console tab |
Switch to Editor | Switch back to the editor. |
[Editor] Click on
|
Under Practice-JS folder, open the file named main.js |
[Editor] Type:
function outer(x) { return function inner(y) { return x + y; }; }
const innerReturnedValue = outerReturnedValue(5);
|
In the main.js file, update the code as shown. |
[Editor] Highlight:
const count = 1; |
Here, I’m declaring a variable count and assigned it the value 1 which I’ll be using later. |
[Editor] Highlight:
return function inner(y) { return x + y; }; } |
Here, I have declared a function outer which takes a variable x as a parameter.
|
[Editor] Highlight:
return x + y; }; |
The returned function is named as inner.
|
[Editor] Highlight:
|
Here, I try to execute the outer function by passing 4 to it.
|
[Editor] Highlight:
|
Here, I execute the outer returned function stored in outerReturnedValue variable.
|
Only Narration | The question here is what is the value of x?
|
[Editor] Highlight:
|
Here, I’m logging outerReturnedValue to check if we have access to it.
|
[Editor] Highlight:
|
Here , I’m logging innerReturnedValue to check what is being returned. |
[Editor] Highlight:
|
Finally, add the count with the innerReturnedValue and store it in a sum variable. |
[Editor] Highlight:
|
And here we are logging sum. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser.
Observe the output. |
[Browser] [Console Tab]:
Highlight:
|
Unfortunately, if you expand the right arrowhead symbol in Firefox, you cannot see the closure.
|
[Display Picture]
|
I have taken this screenshot from Chrome browser, where you can see the highlighted text.
|
[Browser] [Console Tab]:
|
So, here as we have access to x and y, the returned value is 9. |
[Browser] [Console Tab]:
Highlight:
|
So finally, adding the count variable, the sum is 10. |
Only Narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt:
|
Slide: Assignment | As an assignment,
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum. |
Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education (MoE), Government of India |
Slide: Thanks | The script for this tutorial is contributed by Jayesh.
And this is Praveen from IIT Bombay signing off. Thank you for joining. |