Exploring Advanced JavaScript Features
Closures, Prototypal Inheritance, Promises, Async/Await, and Destructuring
JavaScript is a versatile language that can be used for both client-side and server-side scripting. It has evolved over the years to become one of the most popular programming languages in the world. Advanced JavaScript is all about using the language’s more advanced features to build powerful applications. In this blog post, we’ll explore some of these features and give examples of how to use them.
Closures
Closures are an essential concept in advanced JavaScript. They allow you to define a function inside another function and access the outer function’s variables. This is useful when you want to create private variables that are inaccessible to the outside world. Here’s an example:
function outerFunction() {
var privateVariable = "I am private";
function innerFunction() {
console.log(privateVariable);
}
return innerFunction;
}
var innerFunc = outerFunction();
innerFunc(); // Output: "I am private"In this example, outerFunction creates a private variable privateVariable and a nested function innerFunction. outerFunction then returns innerFunction. When we call outerFunction, it returns innerFunction, and we store it in a variable called innerFunc. When we call innerFunc, it logs the value of privateVariable, which is "I am private". This is possible because of closures, which allow innerFunction to access the variables of its parent function, outerFunction.
Prototypal Inheritance
JavaScript is a prototype-based language, which means that objects can inherit properties from other objects. Prototypal inheritance allows you to create new objects based on existing ones, which can save you a lot of time and effort. Here’s an example:
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.getFullName = function() {
return this.firstName + " " + this.lastName;
};
var person = new Person("John", "Doe");
console.log(person.getFullName()); // Output: "John Doe"In this example, we create a constructor function called Person, which takes two parameters, firstName and lastName. We then add a method called getFullName to the Person prototype. This method returns the person's full name by concatenating their first and last name. Finally, we create a new Person object called person and log its full name to the console. Note that person inherits the getFullName method from its prototype, which is why we can call it on the person object.
Promises
Promises are a powerful feature in advanced JavaScript that allow you to handle asynchronous operations in a more structured way. A promise represents the eventual completion (or failure) of an asynchronous operation and allows you to attach callbacks to it. Here’s an example:
function getData() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("Data has been retrieved successfully!");
}, 2000);
});
}
getData()
.then(function(data) {
console.log(data);
})
.catch(function(error) {
console.log(error);
});In this example, we create a function called getData that returns a new promise. Inside the promise, we use setTimeout to simulate an asynchronous operation that takes two seconds to complete. When the operation is complete, we call resolve with a message indicating that the data has been retrieved successfully. We then call getData and attach two callbacks to it using the then and catch methods.
Get Manoranjan Jena’s stories in your inbox
Join Medium for free to get updates from this writer.
The then method is called when the promise is resolved, and it receives the resolved value as its argument. In this case, the resolved value is the message we passed to resolve, which is "Data has been retrieved successfully!". We log this message to the console. The catch method is called when the promise is rejected, and it receives the rejection reason as its argument. We're not using the reject method in this example, so the catch method won't be called.
Async/Await
Async/await is a more recent addition to JavaScript that makes it even easier to work with promises. Async functions are functions that return promises, and they allow you to use the await keyword to wait for a promise to be resolved before continuing with the rest of the code. Here's an example:
function getData() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("Data has been retrieved successfully!");
}, 2000);
});
}
async function printData() {
try {
const data = await getData();
console.log(data);
} catch (error) {
console.log(error);
}
}
printData();In this example, we create a function called printData that is marked as async. Inside the function, we call getData using the await keyword, which means that the function will wait for the promise to be resolved before continuing with the rest of the code. We then log the resolved value to the console. Note that we're using a try/catch block to handle any errors that might occur.
Destructuring
Destructuring is a powerful feature in JavaScript that allows you to extract values from arrays or objects and assign them to variables. This can save you a lot of time and effort when working with complex data structures. Here’s an example:
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345"
}
};
const { firstName, lastName, address: { city } } = person;
console.log(firstName); // Output: "John"
console.log(lastName); // Output: "Doe"
console.log(city); // Output: "Anytown"In this example, we create an object called person with several properties, including an address object. We then use destructuring to extract the firstName, lastName, and city values from the person object and assign them to variables with the same names. Note that we're also using destructuring to extract the city value from the nested address object.
Conclusion
Advanced JavaScript is a vast topic that can take years to master fully. However, by understanding some of the language’s more advanced features, such as closures, prototypal inheritance, promises, async/await, and destructuring, you can create powerful applications that are efficient, easy to maintain, and scalable. I hope this blog post has given you a good introduction to some of these features and how to use them. Happy coding!







