toString method in JavaScript - JavaScript Tutorial

Publié le: 23 janvier 2022
sur la chaîne: Programming for Everybody
79
4

Full source code download link: https://programming4everybody.com/jsl...
----------------------------------------------------
Hello, in this lesson will see, how the method toString in JavaScript works and how we can modify this method.

Each object in JavaScript contains a toString method, and this method returns a textual representation of the object.

We have a class called Car, which contains brand, color and maximum speed. We create an instance of the Car class named anyCar and add arguments to the constructor.

We write the instance named anyCar in the document. Now we try to open the page and see that the toString method only shows the Object object for the anyCar instance.

However, we can easily rewrite and modify the toString method so that this method returns the result we need. In JavaScript, we can easily override the default method by rewriting this method.

So now we uncomment our written toString method, in which we return the values ​​we want to display.

We will try to run the page again and see that the rewritten toString method now displays the properties of the anyCar instance with a text description. That is, exactly as we entered it in the toString function.

Thank you for watching this video and if you like, subscribe to this channel and put like.


Sur cette page du site, vous pouvez voir la vidéo en ligne toString method in JavaScript - JavaScript Tutorial durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Programming for Everybody 23 janvier 2022, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 79 fois et il a aimé 4 téléspectateurs. Bon visionnage!