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 “”.
  • 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 !!!




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