- 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.
Then I override the display function with emp2 object at the runtime as follows.
So, you will see here, we can have all 3 output as intended which is very convenient. Because, we can dynamically change the output with our requirements.
- 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.
Instead of writing single lines to get the values of both PI and SQRT value of 9, we can use second way easily.
We can consume only the required properties of the object to perform our function.
You can see here, although Student consumes so many properties, we can consume 3 of them as we required.
We can have separated array from the existing one by using “…”. Let’s see with the example.
Merging 2 arrays:
Like this we can copy one object to other object as well.
- 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”.
The output says as “undefined”. Because, after we are invoking fetchWebpage(), we do not wait for its completion and we tell to print the response on console about length of responsedata.
- 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 !!!