How can we use a JavaScript function as an object?

JavascriptWeb DevelopmentFront End Technology

To use JavaScript function as an object, you can try to run the following code −

Example

<html>
   <head>
      <title>User-defined objects</title>
      <script>
         function book(title, author){
            this.title = title;
            this.author = author;
         }
      </script>
   </head>
   <body>
      <script>
         var myBook = new book("Amit", "Python");
         document.write("Book title is : " + myBook.title + "<br>");
         document.write("Book author is : " + myBook.author + "<br>");
      </script>
   </body>
</html>
raja
Published on 23-Feb-2018 08:17:15
Advertisements