Difference between revisions of "JavaScript/C4/Arrow-function-in-JS/English"
(Created page with "Title of the script: Arrow Functions Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: JavaScript, HTML, this, Arrow, function {| border =...") |
Nancyvarkey (Talk | contribs) |
||
(3 intermediate revisions by 2 users 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, this, Arrow, function | Keywords: JavaScript, HTML, this, Arrow, function | ||
Line 24: | Line 24: | ||
Learning Objectives | Learning Objectives | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | |||
− | |||
* '''setInterval''' | * '''setInterval''' | ||
* '''clearInterval''' | * '''clearInterval''' | ||
* '''setTimeout '''and | * '''setTimeout '''and | ||
* '''Arrow Functions '''in''' JS''' | * '''Arrow Functions '''in''' JS''' | ||
− | |||
− | |||
|- | |- | ||
|| 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.45.0 ('''code editor''') | * '''Visual Studio Code''' version 1.45.0 ('''code editor''') | ||
Line 47: | Line 41: | ||
|| 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 60: | Line 50: | ||
* Pls download and extract the files. | * 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 71: | Line 59: | ||
* whereas the normal '''function '''has '''‘this’ context'''. | * whereas the normal '''function '''has '''‘this’ context'''. | ||
* The best part of '''arrow functions''' is, it avoids confusion of ‘'''this’ context '''in '''callbacks'''. | * The best part of '''arrow functions''' is, it avoids confusion of ‘'''this’ context '''in '''callbacks'''. | ||
− | |||
− | |||
|- | |- | ||
Line 126: | Line 112: | ||
Press: Alt + L and Alt + O | Press: Alt + L and Alt + O | ||
− | || Save the file and | + | || Save the file and start the '''Live server'''. |
|- | |- | ||
Line 133: | Line 119: | ||
Point to the browser '''Console''' tab | Point to the browser '''Console''' tab | ||
− | || In the web browser, open the '''Browser developer tools''' panel and go to the '''console tab'''. | + | || In the '''web browser''', open the '''Browser developer tools''' panel and go to the '''console tab'''. |
|- | |- | ||
Line 222: | Line 208: | ||
− | Here, ‘'''this’ '''refers to the '''scope '''of the '''display function''' which can be used | + | Here, ‘'''this’ '''refers to the '''scope '''of the '''display function''' which can be used whenever necessary. |
|- | |- | ||
Line 341: | Line 327: | ||
|| Only Narration | || Only Narration | ||
|| Now , let’s see the output. | || Now , let’s see the output. | ||
+ | |||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 447: | Line 432: | ||
− | '''setInterval function '''has some | + | '''setInterval function '''has some major changes, which I’ll be explaining to you. |
|- | |- | ||
Line 516: | Line 501: | ||
|| Only Narration | || Only Narration | ||
|| Now, let's see the output. | || Now, let's see the output. | ||
+ | |||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 556: | Line 540: | ||
|| Slide''': '''Summary''' ''' | || Slide''': '''Summary''' ''' | ||
|| In this tutorial, we have learnt: | || In this tutorial, we have learnt: | ||
− | |||
− | |||
* '''setInterval''' | * '''setInterval''' | ||
* '''clearInterval''' | * '''clearInterval''' | ||
* '''setTimeout '''and | * '''setTimeout '''and | ||
* '''Arrow Function '''in''' JS''' | * '''Arrow Function '''in''' JS''' | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Assignment | || Slide: Assignment | ||
|| As an assignment: | || As an assignment: | ||
− | |||
− | |||
* Open the file '''assignment.js''' which you have created earlier. | * Open the file '''assignment.js''' which you have created earlier. | ||
* Clear the existing code. | * Clear the existing code. | ||
Line 576: | Line 554: | ||
* Open the file '''MyPage.html''' in a web browser. | * 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 584: | Line 560: | ||
* 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 592: | Line 566: | ||
* 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. | ||
− | |||
− | |||
|- | |- |
Latest revision as of 11:06, 21 June 2021
Title of the script: Arrow Functions
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Ankita Maske
Novice Reviewer: Praveeen S.
Keywords: JavaScript, HTML, this, Arrow, function
|
|
Slide: Title | Hello and welcome to the spoken tutorial on “Arrow Function 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: Arrow Function |
|
Only Narration | Now, let us take an example and understand this better. |
Show VS editor | Open Visual Studio Code editor |
[Editor] Welcome Page -> Open Folder -> Practice-JS -> index.html & main.js
|
Then open the files index.html and main.js as explained in the earlier tutorials.
|
[Editor] Type:
<html lang="en"> <head> <title>Arrow Functions</title> </head> <body> <script src="main.js"></script> </body> </html> |
In the index.html file, replace the code as shown.
|
Press: Ctrl + S
|
Save the file and start the Live server. |
[Firefox] Press Ctrl + Shift + I
|
In the web browser, open the Browser developer tools panel and go to the console tab. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
this.speed = 0; var self = this; var stop = setInterval(function () { self.speed++; console.log(self.speed); }, 300);
clearInterval(stop); }, 5000); } var d1 = new Display(); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I’m making a function call with new binding. |
[Editor] Highlight:
this.speed = 0; var self = this; var stop = setInterval(function () { self.speed++; console.log(self.speed); }, 300);
clearInterval(stop); }, 5000); } |
Here, I’m declaring a display function,
|
[Editor] Highlight:
|
In the functional scope, I have declared speed property and assigned it to 0. |
[Editor] Highlight:
|
Here, I’m creating a variable self and assigning ‘this’ context to it.
|
[Editor] Highlight:
self.speed++; console.log(self.speed); }, 300); |
In JS we have an inbuilt method setInterval and it accepts two parameters.
|
[Editor] Highlight:
self.speed++; console.log(self.speed); } |
This is a callback function or you can even call it as an anonymous function.
|
[Editor] Highlight:
|
Here, I’m increasing the value of speed, which is declared in the display function.
|
[Editor] Highlight:
|
In the console, I will log the value of speed. |
[Editor] Highlight:
|
Here, I have kept 300 milliseconds as the time interval.
|
[Editor] Highlight:
clearInterval(stop); }, 5000); |
In JS, setTimeout is also an inbuilt function.
|
[Editor] Highlight:
|
Let’s go inside the callback function.
|
[Editor] Highlight:
|
5000 milliseconds is equivalent to 5 seconds.
|
Only Narration | Now , let’s see the output.
|
Press: Ctrl + S
Switch to Browser |
Save the file and switch to the browser. |
[Browser] [Console Tab]:
Highlight:
2 3 4 . . . . . . . . . . . . . 16 |
Observe here, we can see numbers from 1 to 16.
|
Only Narration | So far, we just learnt about setInterval, setTimeout and clearInterval
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
this.speed = 0; var stop = setInterval(() => { this.speed++; console.log(this.speed); }, 300); setTimeout(() => clearInterval(stop), 5000); }
|
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I do a similar function call like done in the previous example. |
[Editor] Highlight:
this.speed = 0; var stop = setInterval(() => { this.speed++; console.log(this.speed); }, 300); setTimeout(() => clearInterval(stop), 5000); } |
In this function declaration, I have created a speed property in the display function context.
|
[Editor] Highlight:
this.speed++; console.log(this.speed); } |
Here, the callback function is an arrow function.
|
[Editor] Highlight:
|
So, I’m able to access this.speed using the this keyword itself.
|
[Editor] Highlight:
|
In the console, I will log the value of speed. |
[Editor] Highlight:
|
Here, notice setTimeout.
|
Only Narration | Now, let's see the output.
|
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
2 3 4 . . . . . . . 16 |
We get the same output here with the use of the arrow function.
|
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 signing off. Thank you for joining |