[Go to site: main page, start]

0% found this document useful (0 votes)
109 views19 pages

Advanced JavaScript: DOM & Async Concepts

The document discusses various JavaScript topics including DOM manipulation, classes and inheritance, promises, asynchronous JavaScript, AJAX calls, callbacks, iterators, generators, and promises. It provides definitions and examples for key concepts like the DOM, DOM tree structure, DOM methods, asynchronous vs synchronous code, callback functions, promises, iterators, generators, and how to stop a generator function. It compares synchronous and asynchronous JavaScript and explains how promises and async/await help solve callback hell issues.

Uploaded by

aepatil74
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views19 pages

Advanced JavaScript: DOM & Async Concepts

The document discusses various JavaScript topics including DOM manipulation, classes and inheritance, promises, asynchronous JavaScript, AJAX calls, callbacks, iterators, generators, and promises. It provides definitions and examples for key concepts like the DOM, DOM tree structure, DOM methods, asynchronous vs synchronous code, callback functions, promises, iterators, generators, and how to stop a generator function. It compares synchronous and asynchronous JavaScript and explains how promises and async/await help solve callback hell issues.

Uploaded by

aepatil74
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
  • JavaScript Advance Topics
  • Difference between DOM object and WINDOW object
  • What is DOM?
  • The DOM Tree Structure
  • More About DOM
  • Asynchronous JavaScript
  • What are AJAX calls?
  • Callback Function
  • For-of loop and for-each method
  • Callback Hell or Pyramid of Doom
  • Promises
  • Iterators & Generators

JavaScript Advance

Topics
Dom, Dom Manipulation, Setting CSS Styles using JavaScript,
Classes and Inheritance, Iterators and Generators, Promise, Client-
Server Communication, Fetch, Asynchronous JavaScript
Difference b/w DOM object and WINDOW object

🏷 setInterval() and setTimeout() are methods of “window”.


🏷 document is an object in “window”.

🏷 document object does have various object, methods or properties like


[Link]().
🏷 window Object has properties like innerHeight and innerWidth.
What is DOM?

DOCUMENT OBJECT MODEL: STRUCTURED


REPRESENTATION OF HTML DOCUMENTS.
ALLOWS JAVASCRIPT TO ACCESS HTML
ELEMENTS AND STYLES TO MANIPULATE
THEM.

Change text, HTML


attributes, and even
CSS styles.
THE DOM TREE STRUCTURE
Special object that is the
entry point to the DOM.
Example:
[Link]().
MORE ABOUT DOM

DOM Methods and


Properties for DOM
Manipulation Not a part of

For example
[Link]()
WEB APIs are basically libraries
that are also written in JS and are
automatically available for you to
use.

Timers Can Interact


with
Fetch
ASYNCHRONOUS JAVASCRIPT

Synchronous JavaScript
THREAD OF
EXECUTIO
N

Blocking

👉 Most code is synchronous;


Part of execution context that
👉 Synchronous code is executed line by line; actually executes the code in
👉 Each line of code waits for previous line to finish; computer’s CPU

👎 Long-running operations block code execution.


ASYNCHRONOUS CODE

THREAD OF BACKGROUND
EXECUTIO
Callback will run
N
after timer Timer
running

👉 Asynchronous code is executed after a task that runs in the “background”


finishes.
👍 Asynchronous code is non blocking.
👉 Execution doesn’t wait for an asynchronous task to finish its work.
What are AJAX calls?

Asynchronous JavaScript And XML: Allows us to communicate


with remote web servers in an asynchronous way. With AJAX
calls, we can request data from web servers dynamically.

Asking for some data

REQUEST WEB
CLIENT
SERVER
💻
(e.g. Browser)
🌐
RESPONSE
Sending Data Back
Callback Function

Example of Callback

Callback Function is a function as


a parameter in another function.

E.g.: fname(function(param1, param2, ...., paramN){


//statements
})
For-of loop and for-each () method

For (variable of iterable){


}

[Link](function(value,index,array)

So we use other methods such as


promises and async/await
Callback Hell or Pyramid of Doom

It becomes unmanageable when we have


multiple calls depending on the result

So we use other methods such as


promises and async/await
Promises

A Simple Promise

The Promise object represents the


eventual completion (or failure) of
an asynchronous operation and its
resulting value

A Promise is in one of these states:

👉 pending: Initial state, neither fulfilled nor rejected.


👉 fulfilled: meaning that the operation was successful
👉 rejected: meaning that operation failed
Promisification

Promisifying the previous


callback code
Iterators & Generators

🏷 Iterators were introduced in ES6.

🏷 It is a mechanism to iterate or traverse through data


structures.
🏷 As you know Array is already iterable.

🏷 It means if you want to put a for...of loop on an array, then


you can easily do that.
‍‍💻
Iterators & Generators

let obj ={
value1:number,
You want to use value2:number
for...of loop on }
this object literal

You want to make this object literal an


iterable object.

We can do this in ES6 by using [Link]


Iterators Example

Array Example Object Example

How do you create an iterator?



👉 The “[Link]” method must be �
implemented which should return an iterator
object & also should have a next() method which
returns the object.
Generators

🏷 “Generators” can help you to pause and resume the code.


🏷 Normally when you write function, it returns a single value.

🏷 You can think of generators as a kind of function which can return multiple
values in phases.
🏷 The function* is the keyword used to define a generator function.
🏷 yield is an operator which pauses the generator.
🏷 yield also helps us to receive input & send output.
Generators Example

Example Refactored Iterator


code

How do you create an iterator?


The “[Link]” method must be
implemented which should return an iterator
object & also should have a next() method which
returns the object.
Generators Example

Stop a generator function

Common questions

Powered by AI

The DOM object is a structured representation of the HTML document, allowing JavaScript to access and manipulate HTML elements and styles. Key properties include document.querySelector(). The window object represents the browser window and provides properties such as innerHeight and innerWidth, as well as methods like setInterval() and setTimeout().

DOM manipulation allows JavaScript to interact with the web page's structure by changing text, HTML attributes, and CSS styles. This is achieved through methods and properties accessed via the document object, such as document.querySelector(), enabling dynamic updates to user interfaces based on user interactions or other events .

Synchronous JavaScript executes code line by line, with each line waiting for the previous one to finish. This can lead to blocking of code execution during long-running operations . Asynchronous JavaScript, on the other hand, allows operations to be executed in the background, enabling non-blocking code that doesn't wait for tasks to complete before moving on to the next line. This enhances performance by improving responsiveness and reducing waiting time .

The yield keyword in JavaScript generators is used to pause and resume the execution of a generator function. Unlike traditional functions that return a single value and terminate, generators can yield multiple values over time. When a generator function is called, execution pauses at the yield expression, allowing intermediate states to be captured or awaiting input before continuing .

Promises and async/await improve handling asynchronous operations by providing a more readable code structure compared to callbacks, which can lead to callback hell or Pyramid of Doom when multiple nested callbacks become hard to manage. Promises represent eventual completion of an operation and can be chained, allowing sequential execution or error handling . Async/await further simplifies this by allowing asynchronous code to be written in a synchronous-like manner, making it easier to read and maintain .

Iterators provide a mechanism to traverse data structures in JavaScript. By implementing the Symbol.iterator method, any object can be made iterable, enabling for...of loops . Generators, defined with function* syntax, offer functions that can return multiple values over time using the yield keyword. They allow for pausing and resuming execution, which can be advantageous for generating or consuming a sequence of values without storing them in memory all at once .

Both setInterval() and setTimeout() methods, as part of the window object, are used for executing functions at specified intervals. setInterval() repeatedly calls a function with a fixed time delay, whereas setTimeout() executes a function after a set delay. While these methods are useful for tasks like animations or periodic updates, they can be unreliable due to potential drift over time from varying execution timing or blocked main threads, leading to inaccurate function firing if not properly managed .

ES6 allows any object to be made iterable by implementing the Symbol.iterator method. This method should return an iterator object with a next() method, which returns an object containing value and done properties. This setup allows the object to be traversed using a for...of loop, thus offering more flexibility for custom iteration logic .

The fetch API in JavaScript is used for making network requests and returns a Promise that resolves to the Response object once the request is complete. This integration allows for chaining of then() methods to process the response, handling both the success of data retrieval and potential errors with catch(), providing a cleaner and more manageable approach to asynchronous code compared to callbacks .

AJAX (Asynchronous JavaScript and XML) allows web applications to communicate with remote servers asynchronously, enabling data to be requested dynamically without refreshing the webpage. It involves sending a request to the server and processing the response via callbacks, which allows for an improved user experience by updating parts of a web page with new data without reloading the entire page .

JavaScript Advance 
Topics
Dom, Dom Manipulation, Setting CSS Styles using JavaScript, 
Classes and Inheritance, Iterators an
Difference b/w DOM object and WINDOW object
🏷 setInterval() and setTimeout() are methods of “window”.
🏷 document is an obje
DOCUMENT OBJECT MODEL: STRUCTURED 
REPRESENTATION OF HTML DOCUMENTS. 
ALLOWS JAVASCRIPT TO ACCESS HTML 
ELEMENTS AND STYLES T
 
THE DOM TREE STRUCTURE
Special object that is the 
entry point to the DOM. 
Example:
document.querySelector().
MORE ABOUT DOM
DOM Methods and 
Properties for DOM 
Manipulation
Not a part of 
For example 
document.querySelector()
Can Int
ASYNCHRONOUS JAVASCRIPT
Synchronous JavaScript
 
👉Most code is synchronous;
 
👉Synchronous code is executed line by line;
T
ASYNCHRONOUS CODE
THREAD OF 
EXECUTIO
N
Callback will run 
after timer
BACKGROUND
Timer 
running
 
👉Asynchronous code is exe
What are AJAX calls?
Asynchronous JavaScript And XML: Allows us to communicate 
with remote web servers in an asynchronous wa
Callback Function
Example of Callback
Callback Function is a function as 
a parameter in another function. 
E.g.: fname(funct
For-of loop and for-each () method
For (variable of iterable){
}
Variablename.forEach(function(value,index,array)
So we use o

You might also like