Difference between revisions of "JavaScript/C3/Closure-in-JS/English"
(Created page with "Title of the script:Closure Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: JavaScript, HTML, document, closure, function {| border =...") |
Nancyvarkey (Talk | contribs) |
||
Line 24: | Line 24: | ||
Learning Objectives | Learning Objectives | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | |||
− | |||
* '''Closures '''in''' JS''' | * '''Closures '''in''' JS''' | ||
* And, their '''scope '''in''' function''' | * And, their '''scope '''in''' function''' | ||
− | |||
− | |||
|- | |- | ||
|| Slide: System Specifications | || Slide: System Specifications | ||
|| This tutorial is recorded using: | || This tutorial is recorded using: | ||
− | |||
− | |||
* '''Ubuntu Linux''' OS version 18.04 | * '''Ubuntu Linux''' OS version 18.04 | ||
* '''Visual Studio Code''' version 1.51.1 ('''code editor''') | * '''Visual Studio Code''' version 1.51.1 ('''code editor''') | ||
Line 45: | Line 39: | ||
|| Slide : Pre-requisites | || Slide : Pre-requisites | ||
|| To practice this tutorial, | || To practice this tutorial, | ||
− | |||
− | |||
* You should be familiar with writing and executing''' JS''' files'''.''' | * You should be familiar with writing and executing''' JS''' files'''.''' | ||
* If not, please go through the prerequisite tutorials on this website. | * If not, please go through the prerequisite tutorials on this website. | ||
− | |||
− | |||
|- | |- | ||
Line 56: | Line 46: | ||
|| | || | ||
* The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | * The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | ||
− | * Pls download and extract the | + | * Pls download and extract the files. |
* Make a copy and then use them for practising. | * Make a copy and then use them for practising. | ||
− | |||
− | |||
|- | |- | ||
Line 76: | Line 64: | ||
|| <nowiki>[Editor] </nowiki>Welcome Page -> Open Folder -> '''Practice-JS''' | || <nowiki>[Editor] </nowiki>Welcome Page -> Open Folder -> '''Practice-JS''' | ||
|| In the '''editor, '''browse and open the folder “'''Practice-JS''' ”. | || In the '''editor, '''browse and open the folder “'''Practice-JS''' ”. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 292: | Line 277: | ||
|| Switch to '''Browser''' | || Switch to '''Browser''' | ||
|| Switch back to the '''browser.''' | || Switch back to the '''browser.''' | ||
+ | |||
+ | Observe the output. | ||
|- | |- | ||
Line 304: | Line 291: | ||
This is because the '''Closure '''node is hidden in '''Firefox dev tools'''. | This is because the '''Closure '''node is hidden in '''Firefox dev tools'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 356: | Line 340: | ||
|| Slide''': '''Summary''' ''' | || Slide''': '''Summary''' ''' | ||
|| In this tutorial, we have learnt: | || In this tutorial, we have learnt: | ||
− | |||
− | |||
* '''Closures '''in''' JS''' | * '''Closures '''in''' JS''' | ||
* And, their '''scope '''in''' function''' | * And, their '''scope '''in''' function''' | ||
− | |||
− | |||
|- | |- | ||
Line 372: | Line 352: | ||
* '''Declare''' a '''variable data '''using '''let keyword''' | * '''Declare''' a '''variable data '''using '''let keyword''' | ||
* '''Return''' a '''display function '''accepting a '''variable '''as '''parameter ''' | * '''Return''' a '''display function '''accepting a '''variable '''as '''parameter ''' | ||
− | * Return the '''sum '''of '''data '''and '''variable''' | + | * '''Return''' the '''sum '''of '''data '''and '''variable''' |
* '''Log''' the '''value '''in the '''console''' | * '''Log''' the '''value '''in the '''console''' | ||
− | * Open the file '''MyPage.html '''in a '''web | + | * Open the file '''MyPage.html '''in a '''web browser'''. |
* Observe the output in the '''browser’s console''' | * Observe the output in the '''browser’s console''' | ||
− | |||
− | |||
|- | |- | ||
Line 384: | Line 362: | ||
* The video at the following link summarises the Spoken Tutorial project. | * The video at the following link summarises the Spoken Tutorial project. | ||
* Please download and watch it | * Please download and watch it | ||
− | |||
− | |||
|- | |- | ||
Line 392: | Line 368: | ||
* We conduct workshops using spoken tutorials and give certificates. | * We conduct workshops using spoken tutorials and give certificates. | ||
* For more details, please write to us. | * For more details, please write to us. | ||
− | |||
− | |||
|- | |- |
Revision as of 15:17, 19 January 2021
Title of the script:Closure
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer:
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 Browser 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. |