Table of contents
No headings in the article.
In JavaScript, all functions are object methods.
If a function is not a method of a JavaScript object, it is a function of the global object.
We can discuss some methods here and will try to understand them properly with the help of practical examples.
const AirIndia = {
airline: "Tata",
iataCode: "TH",
bookings : [],
book( flightNum , name){
console.log(
`${name} booked a seat on ${this.airline} flight ${this.iataCode} ${flightNum}`
);
this.bookings.push({flight : `${this.iataCode} ${flightNum}` , name });
},
};
AirIndia.book(239 , "Jayesh Hande" ); // Jayesh Hande booked a seat on AirIndia flight TH239
AirIndia.book(723 , "Kunal Mirgal" ); // Kunal Mirgal booked a seat on AirIndia flight TH239
console.log(AirIndia);
- Now we will create a copy of the AirIndia function and store it in another variable book which also works as a function.
const book = AirIndia.book;
- but we can't access the book function from the AirIndia object and it gives a TypeError.
- because it works as a regular function call and this keyword from the AirIndia object is not accessible.
- Hence this keyword depends on how the function is called.
- Well, basically we need to tell JavaScript explicitly what's the dispute here should be like so if we want to book a lot of the flights.
const eurowings = {
airline: "Eurowings",
iataCode : "EW" ,
bookings : [] ,
};
1) Call() method
book.call(eurowings , 22 , "Jayesh Hande" );
console.log( eurowings );
book.call(AirIndia , 33 , "Kunal Mirgal" );
console.log( AirIndia )
- By using the call () method we tell the function who calls the function by just passing the object as an argument along with the other parameters.
- It helps to avoid writing unnecessary duplicate code and also we can call declared functions by different objects.
- we are also able to use the this keyword in objects by normal function
2) Apply() method
- It does not receive a list of arguments instead it takes an array of arguments.
const flightData = [33 , "Kunal Mirgal"];
book.apply(eurowings , flightData);
console.log(eurowings);
- but now there is a better way to pass an array rather than using apply method. By using spread operator
book.call(eurowings , ...flightData)
console.log(eurowings);
- It works the same as apply() method.
3) Bind() method
- Bind() method is one of the most important methods in JavaScript.
- We can use the bind method to create a new function with the this keyword.
const bookEW = book.bind(eurowings);
bookEW (27, "Akshad Jaiswal")
- This will not be called the book function. Instead, it will return a new function
- This now looks like the normal book function call again, and that's because in this function you already have the this keyword set in stone, basically.
- So, we no longer need to specify the this keyword again. That's the beauty of the bind() function.
- bind() method is an important and long topic. so I put a link to mdn docs here so that you can get more detailed knowledge about it. link: "developer.mozilla.org/en-US/docs/Web/JavaSc.."
If you reach here it means you explore all the blog and thank you for this😊. If you like this then please press the like💛 button and follow me to get such information about JavsScript.