JavaScript call and apply methods in action

Publié le: 20 avril 2024
sur la chaîne: Mayank Srivastava
291
9

In this code snippet, you have an object named person with two properties:
name: It holds the value "john".
getName: It's a function that returns the name property of the person object using this.
Next, you declare a variable fetchName and assign the getName method of the person object to it.

Finally, you call fetchName().

However, there's a caveat here: when fetchName is called, it's no longer associated with the person object. So, the this keyword inside fetchName doesn't refer to the person object anymore. Instead, it refers to the global object (in non-strict mode) or undefined (in strict mode). Since the global object (or undefined in strict mode) doesn't have a name property, calling fetchName() will likely result in an error or undefined being returned.


Sur cette page du site, vous pouvez voir la vidéo en ligne JavaScript call and apply methods in action durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Mayank Srivastava 20 avril 2024, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 291 fois et il a aimé 9 téléspectateurs. Bon visionnage!