Modern Javascript — Part 2

  • In 2015, ECMAScript 6 introduces Javascript classes. In java, we define our constructor of the class with the class name. But in Javascript (JS), we have used “constructor” keyword to make the constructor with the paramerters without defining any local variables for the class.
  • In JS, we can override the function of the class at runtime if you need. Let’s see via example.
  • I have created 2 classes called Employee and Manager which Manager extends the Employee class. As well, Employee constructor has name variable while Manager constructor consists with both name and section values.
  • In De structuring, we can unpack the values from object or arrays in to variables easily.
  • There are several ways we can use the de structuring behavior in java script. Let’s see them one by one.
  • In JS, we can use promise to handle the one function after getting results from the promise object. The function waits for results from promise object is named as call-back function.
  • In the above code sample, you can see that we need to get the length of data after fetching the “https://nirodhajayaweera1993.medium.com/”.
  • I have function called fetchWebpage and inside that our promise objects with 2 values known as “resolve and reject”. Resolve means when we get the intended outcome and reject means when it is unable to create our request.
  • We need to wrap our promise object because of the tasks of promise gets some time to complete. Otherwise, our application will not provides our intended outcomes.
  • That means the response will be send only if the wrapper finishes its tasks.
  • You can see that after we called fetchWebpage() at the bottom of our program, I used “.then ”to print output of the program in to my console. The use of “.then” means, we are telling that get this output after completing fetchWebpage() completely.
  • Let me create another function and called fetchWebpage() without specifying “.then”.
  • Also, instead of using “.then” we can use 2 keywords known as “await” and “async”. Let me do this with my code sample.
  • What happens here is, “awaits” tells our function to wait until promise resolves.
  • “await” keyword is only used within the “async ” functions.

Stay Safe !!!

--

--

--

Associate Software Engineer at Virtusa.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

React JS and Experience during group project

Async JS, wait what?

Setup Angular 12 vs 11 vs 10 features benchmark

Setup Angular 12 vs 11 vs 10 features benchmark

From JS to React

Build TypeForm from scratch using React with Material UI

Gatsby JS: 8 of the Most Common Mistakes That Developers Make

Generating PDFs in Javascript for fun and profit!

6 Visual Studio Code Extensions I Cannot Live Without

VS Code extension icons

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sanduni Jayaweera

Sanduni Jayaweera

Associate Software Engineer at Virtusa.

More from Medium

Implementing a Stack in Vanilla Javascript

How to append values to Javascript Map object

Using JavaScript to Update the DOM on Dropdown Selection: One Example

Devslopes: Main Function of Javascript