SlideShare a Scribd company logo
Tutorial - 7
• call, apply & bind
• pollyfill
• debouncing
• throttling
• currying
• debouncing examples
• currying examples
• async & defer
• event bubbling & Trickling
• event delegation
• prototype & prototypal
inheritance
• CORS, Prefflight request
• iterators
• generators
call, apply & bind
• They all are used for performing function
borrowing
• Function borrowing happens between a
function and object
⚬ That function can exist inside another object
(CASE - 1) or outside all together (CASE-2)
CASE-1
Case-2
call, apply &
bind
without
parameter
with parameters
• Polyfill for bind:
⚬ is basically the methodology to make our own bind() function when our
browser doesn't support bind()
• Polyfill defination:
⚬ A polyfill is a piece of code that implements the features that you expect
the browser to support natively.
⚬ For better understanding :
■ https://javascript.info/polyfills
Web development basics (Part-6)
Debouncing
• Word debouncing came from electronics where when we click the button of
remote control and if we hit it very frequently it doesn't resend the signal
immediately, instead it holds the execution for few seconds and then execute
the control.
Debouncing in JS
• Similarly, when we have same kind of operation in
web-development, e.g. searching or scroll-event
or screen-resizing and if those operations are
associated with an API call, then we use
debouncing for improving browser performance.
• Let's see the code effect:
Without debouncing search operation
Web development basics (Part-6)
Web development basics (Part-6)
Throttle
Throttle in JS
• Throttle in JS means when there is a scenario that there are many
requests aligned and with intention to optimize the browser
speed, we ignore all of the requests which are coming under a
given time span.
• e.g: We are searching "Samsung Note 8" in a search bar
⚬ As soon as we have started typing it with a time span of 300ms
then, we have typed S[amsun]g, so everything took for
example as 400ms and [amsung] took 300ms so, NO API
CALL will happen for [amsung], it will just be started with S
and when g will be typed then only a call will happen
Web development basics (Part-6)
Debouncing vs Throttling
• Lets consider time span of 300 ms in either cases, so
• Debouncing: means your request will occur with time gap of 300 ms, i.e. If you
are typing "Hello World", then
⚬ if you are typing it as Hello[pause of 300 ms] <space> World
• Hence if the user is taking a pause and that pause is >=300ms so in that time
being no API call will occur
• Throttling: means that when there are many requests e.g. user made
consecutive 100 requests and timespan is of 10ms so there will be any request
only after each 10 seconds only, hence in between requests are throttled or
cancelled or killed
Function currying
• Function currying can be achieved by 2 ways in JS:
⚬ Using function's closures
⚬ Using bind()
• It basically means to execute a function of any other
function
• Its depth may reach to any extent, depending on
user requirement
Function currying using bind method
Function currying with closure
Async vs defer vs normal <script>
• When parser runs on html then we have the parser running on it, which results in parsing
as following :
• Normal:
⚬ First some of the HTML will be fetched but it will be hindered by fetching of
subsequent JS hence bad UX with partial HTML will be on DOM
• Async:
⚬ First partial fetching along with pre-fetching of JS will happen then execution will
happen
■ Use case - when scripts are not inter-dependent on each other
• Defer
⚬ First all of HTML fetching will happen then, JS fetch followed by execution will happen
■ Use case- when scripts are interdependent on each other
https://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html
Web development basics (Part-6)
Event bubbling & capturing (or trickling)
• When we have an hierarchy of elements on DOM and
an event occurs then it can traverse in 2 motions only:
⚬ Top to bottom i.e. Capturing or trickling
⚬ Bottom to Top i.e. Event bubbling
• Netscape favored Capturing as main fundamental in past
• While, Microsoft favored Bubbling
• Finally W3 recommended that it would be entirely
developer's choice only that what he likes to do
• By default Bubbling occurs in all browsers
• If both have been used together then Capturing is at higher
precedence to occur
• Syntax:
document.getElementById("child").addEventListener("click",
() => console.log("child clicked"), false)
Example-1
Example-2
Example-3
• e.stopPropogation()
• Sometimes we need to stop the propogation of flow at any point so we need
e.stopPropogation() there.
Event delegation
• Event delegation is the methodology according to which a component
uses Event bubbling and help us to let parent element's event-handler
listen any event occuring on any of that parent's children element.
• Hence, it helps in optimization w.r.t. (PROS)
⚬ Speed of our application
⚬ Saving memory
⚬ And letting us write readable code with less number of LOC
• Conditions to be considered while applying event delegation are: (CONS)
⚬ We must be aware about usage of stopPropagation()
⚬ Not all events are bubbled up hence not all events supports event
delegation too
Web development basics (Part-6)
Prototype, Prototypal inheritance & Prototype chain
• Prototype is the prebuilt or custom properties assigned to any data type
(variable, object, array, function...) inside it
• Prototype that are prebuilt can be gained with extention of __proto__
• For example we've to add a custom prototype to Array class so it will be as:
• Predefined prototypes & Prototype chaining
• Prototypal inheritance:
⚬ It is completely different from what inheritance means in other languages
⚬ Modifying existing i.e. predefined prototype is strictly prohibited due to huge
performance issue, so following example is just for demonstration purpose
CORS & Preflight Requests & OPTIONS Method
• CORS- Cross Origin Resource Sharing
• It helps to share various types of resources across several
origins
• By "origin" we mean platforms or hosts or domain of your
application
• CORS solves the problems like:
⚬ Resource sharing across different domains
⚬ Resource sharing across different subdomains
⚬ Resource sharing across different ports
⚬ Resource sharing across different protocols
How CORS Works actually
Whenever a communication occurs
between two separate origins A and B
then 2 major scenarios can be there :
1- normal request & 2- Preflight
request. For CORS preflight requests
(also called OPTIONS request) occur,
which then replied by origin B. While
replying B, will respond by setting
some additional header (usually its
Accept-Control-Allow-Origin), & then
finally the POST request would occur
across origins.
Iterators
Generators
• Generators are the functions that gives/yield any required value
without even considering the storage of the entire set of value
• Its is signified by usage of * between keyword function and that
function's name
• it gives value from the function by usage of keyword yield or
return
• But the only difference between yield and return is execution of
function continues after coming across yield in control flow and
that's not so with return keyword
• Generator function on executing yield will give an object as:
⚬ {next:{ value: <whatever value>, done: <boolean>}}
Example
Example-2

More Related Content

What's hot (19)

PDF
Model with actors and implement with Akka
Ngoc Dao
 
PPTX
Actor-based concurrency and Akka Fundamentals
Ngoc Dao
 
ODP
How to start using Scala
Ngoc Dao
 
PPTX
JS Event Loop
Saai Vignesh P
 
PDF
Back to the future: Isomorphic javascript applications
Luciano Colosio
 
PPTX
Javascript 01 (js)
AbhishekMondal42
 
PPTX
Introduction to JavaScript
Marlon Jamera
 
PDF
JavaScript - Chapter 3 - Introduction
WebStackAcademy
 
PDF
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
PDF
JavaScript Good Practices
Jussi Pohjolainen
 
PDF
Actors and Microservices - Can two walk together? - Rotem Hermon, Gigya
Codemotion Tel Aviv
 
PPTX
Introduction To JavaScript
Reema
 
PDF
Actor model : A Different Concurrency Approach
Emre Akış
 
PDF
Advanced Reflection in Pharo
Marcus Denker
 
PDF
Intro to JavaScript - Thinkful LA, June 2017
Thinkful
 
PDF
Conscious Decoupling - Lone Star PHP
CiaranMcNulty
 
PDF
The Skinny on Slim
Eric Mulligan
 
PPTX
Green Custard Friday Talk 5: React-Native Performance
Green Custard
 
Model with actors and implement with Akka
Ngoc Dao
 
Actor-based concurrency and Akka Fundamentals
Ngoc Dao
 
How to start using Scala
Ngoc Dao
 
JS Event Loop
Saai Vignesh P
 
Back to the future: Isomorphic javascript applications
Luciano Colosio
 
Javascript 01 (js)
AbhishekMondal42
 
Introduction to JavaScript
Marlon Jamera
 
JavaScript - Chapter 3 - Introduction
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
JavaScript Good Practices
Jussi Pohjolainen
 
Actors and Microservices - Can two walk together? - Rotem Hermon, Gigya
Codemotion Tel Aviv
 
Introduction To JavaScript
Reema
 
Actor model : A Different Concurrency Approach
Emre Akış
 
Advanced Reflection in Pharo
Marcus Denker
 
Intro to JavaScript - Thinkful LA, June 2017
Thinkful
 
Conscious Decoupling - Lone Star PHP
CiaranMcNulty
 
The Skinny on Slim
Eric Mulligan
 
Green Custard Friday Talk 5: React-Native Performance
Green Custard
 

Similar to Web development basics (Part-6) (20)

PDF
Intro to Asynchronous Javascript
Garrett Welson
 
PDF
Testing web APIs
FDConf
 
PPTX
asyncjavascript.pptxdgdsgdffgfdgfgfgfdgfdgf
zmulani8
 
PDF
Fast Cordova applications
Ivano Malavolta
 
PPTX
5 x HTML5 worth using in APEX (5)
Christian Rokitta
 
PDF
The evolution of asynchronous JavaScript
Alessandro Cinelli (cirpo)
 
PDF
Performance Improvements In Browsers
GoogleTecTalks
 
PDF
Performance Improvements in Browsers
jeresig
 
PDF
Streams API (Web Engines Hackfest 2015)
Igalia
 
PPTX
Lecture05.pptx
MrVMNair
 
PDF
From Node.js to Design Patterns - BuildPiper
Luciano Mammino
 
PPTX
Advanced Web Technology.pptx
ssuser35fdf2
 
PDF
The Strange World of Javascript and all its little Asynchronous Beasts
Federico Galassi
 
PDF
More efficient, usable web
Chris Mills
 
PPTX
StrongLoop Overview
Shubhra Kar
 
PDF
Douglas Crockford: Serversideness
WebExpo
 
PPTX
Async discussion 9_29_15
Cheryl Yaeger
 
PPTX
ES6 detailed slides for presentation.pptx
AneesLarik1
 
ODP
Object Oriented Javascript
NexThoughts Technologies
 
PDF
JavaScript Core
François Sarradin
 
Intro to Asynchronous Javascript
Garrett Welson
 
Testing web APIs
FDConf
 
asyncjavascript.pptxdgdsgdffgfdgfgfgfdgfdgf
zmulani8
 
Fast Cordova applications
Ivano Malavolta
 
5 x HTML5 worth using in APEX (5)
Christian Rokitta
 
The evolution of asynchronous JavaScript
Alessandro Cinelli (cirpo)
 
Performance Improvements In Browsers
GoogleTecTalks
 
Performance Improvements in Browsers
jeresig
 
Streams API (Web Engines Hackfest 2015)
Igalia
 
Lecture05.pptx
MrVMNair
 
From Node.js to Design Patterns - BuildPiper
Luciano Mammino
 
Advanced Web Technology.pptx
ssuser35fdf2
 
The Strange World of Javascript and all its little Asynchronous Beasts
Federico Galassi
 
More efficient, usable web
Chris Mills
 
StrongLoop Overview
Shubhra Kar
 
Douglas Crockford: Serversideness
WebExpo
 
Async discussion 9_29_15
Cheryl Yaeger
 
ES6 detailed slides for presentation.pptx
AneesLarik1
 
Object Oriented Javascript
NexThoughts Technologies
 
JavaScript Core
François Sarradin
 
Ad

Recently uploaded (20)

PDF
SAP GUI Installation Guide for Windows | Step-by-Step Setup for SAP Access
SAP Vista, an A L T Z E N Company
 
PDF
Salesforce Pricing Update 2025: Impact, Strategy & Smart Cost Optimization wi...
GetOnCRM Solutions
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PDF
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PDF
What companies do with Pharo (ESUG 2025)
ESUG
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
SAP GUI Installation Guide for Windows | Step-by-Step Setup for SAP Access
SAP Vista, an A L T Z E N Company
 
Salesforce Pricing Update 2025: Impact, Strategy & Smart Cost Optimization wi...
GetOnCRM Solutions
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
Presentation about variables and constant.pptx
kr2589474
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
Activate_Methodology_Summary presentatio
annapureddyn
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
What companies do with Pharo (ESUG 2025)
ESUG
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Ad

Web development basics (Part-6)

  • 1. Tutorial - 7 • call, apply & bind • pollyfill • debouncing • throttling • currying • debouncing examples • currying examples • async & defer • event bubbling & Trickling • event delegation • prototype & prototypal inheritance • CORS, Prefflight request • iterators • generators
  • 2. call, apply & bind • They all are used for performing function borrowing • Function borrowing happens between a function and object ⚬ That function can exist inside another object (CASE - 1) or outside all together (CASE-2)
  • 7. • Polyfill for bind: ⚬ is basically the methodology to make our own bind() function when our browser doesn't support bind() • Polyfill defination: ⚬ A polyfill is a piece of code that implements the features that you expect the browser to support natively. ⚬ For better understanding : ■ https://javascript.info/polyfills
  • 9. Debouncing • Word debouncing came from electronics where when we click the button of remote control and if we hit it very frequently it doesn't resend the signal immediately, instead it holds the execution for few seconds and then execute the control.
  • 10. Debouncing in JS • Similarly, when we have same kind of operation in web-development, e.g. searching or scroll-event or screen-resizing and if those operations are associated with an API call, then we use debouncing for improving browser performance. • Let's see the code effect:
  • 15. Throttle in JS • Throttle in JS means when there is a scenario that there are many requests aligned and with intention to optimize the browser speed, we ignore all of the requests which are coming under a given time span. • e.g: We are searching "Samsung Note 8" in a search bar ⚬ As soon as we have started typing it with a time span of 300ms then, we have typed S[amsun]g, so everything took for example as 400ms and [amsung] took 300ms so, NO API CALL will happen for [amsung], it will just be started with S and when g will be typed then only a call will happen
  • 17. Debouncing vs Throttling • Lets consider time span of 300 ms in either cases, so • Debouncing: means your request will occur with time gap of 300 ms, i.e. If you are typing "Hello World", then ⚬ if you are typing it as Hello[pause of 300 ms] <space> World • Hence if the user is taking a pause and that pause is >=300ms so in that time being no API call will occur • Throttling: means that when there are many requests e.g. user made consecutive 100 requests and timespan is of 10ms so there will be any request only after each 10 seconds only, hence in between requests are throttled or cancelled or killed
  • 18. Function currying • Function currying can be achieved by 2 ways in JS: ⚬ Using function's closures ⚬ Using bind() • It basically means to execute a function of any other function • Its depth may reach to any extent, depending on user requirement
  • 19. Function currying using bind method
  • 21. Async vs defer vs normal <script> • When parser runs on html then we have the parser running on it, which results in parsing as following : • Normal: ⚬ First some of the HTML will be fetched but it will be hindered by fetching of subsequent JS hence bad UX with partial HTML will be on DOM • Async: ⚬ First partial fetching along with pre-fetching of JS will happen then execution will happen ■ Use case - when scripts are not inter-dependent on each other • Defer ⚬ First all of HTML fetching will happen then, JS fetch followed by execution will happen ■ Use case- when scripts are interdependent on each other https://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html
  • 23. Event bubbling & capturing (or trickling) • When we have an hierarchy of elements on DOM and an event occurs then it can traverse in 2 motions only: ⚬ Top to bottom i.e. Capturing or trickling ⚬ Bottom to Top i.e. Event bubbling • Netscape favored Capturing as main fundamental in past • While, Microsoft favored Bubbling • Finally W3 recommended that it would be entirely developer's choice only that what he likes to do • By default Bubbling occurs in all browsers
  • 24. • If both have been used together then Capturing is at higher precedence to occur • Syntax: document.getElementById("child").addEventListener("click", () => console.log("child clicked"), false)
  • 28. • e.stopPropogation() • Sometimes we need to stop the propogation of flow at any point so we need e.stopPropogation() there.
  • 29. Event delegation • Event delegation is the methodology according to which a component uses Event bubbling and help us to let parent element's event-handler listen any event occuring on any of that parent's children element. • Hence, it helps in optimization w.r.t. (PROS) ⚬ Speed of our application ⚬ Saving memory ⚬ And letting us write readable code with less number of LOC • Conditions to be considered while applying event delegation are: (CONS) ⚬ We must be aware about usage of stopPropagation() ⚬ Not all events are bubbled up hence not all events supports event delegation too
  • 31. Prototype, Prototypal inheritance & Prototype chain • Prototype is the prebuilt or custom properties assigned to any data type (variable, object, array, function...) inside it • Prototype that are prebuilt can be gained with extention of __proto__ • For example we've to add a custom prototype to Array class so it will be as:
  • 32. • Predefined prototypes & Prototype chaining
  • 33. • Prototypal inheritance: ⚬ It is completely different from what inheritance means in other languages ⚬ Modifying existing i.e. predefined prototype is strictly prohibited due to huge performance issue, so following example is just for demonstration purpose
  • 34. CORS & Preflight Requests & OPTIONS Method • CORS- Cross Origin Resource Sharing • It helps to share various types of resources across several origins • By "origin" we mean platforms or hosts or domain of your application • CORS solves the problems like: ⚬ Resource sharing across different domains ⚬ Resource sharing across different subdomains ⚬ Resource sharing across different ports ⚬ Resource sharing across different protocols
  • 35. How CORS Works actually Whenever a communication occurs between two separate origins A and B then 2 major scenarios can be there : 1- normal request & 2- Preflight request. For CORS preflight requests (also called OPTIONS request) occur, which then replied by origin B. While replying B, will respond by setting some additional header (usually its Accept-Control-Allow-Origin), & then finally the POST request would occur across origins.
  • 37. Generators • Generators are the functions that gives/yield any required value without even considering the storage of the entire set of value • Its is signified by usage of * between keyword function and that function's name • it gives value from the function by usage of keyword yield or return • But the only difference between yield and return is execution of function continues after coming across yield in control flow and that's not so with return keyword • Generator function on executing yield will give an object as: ⚬ {next:{ value: <whatever value>, done: <boolean>}}