Computer >> Computer tutorials >  >> Programming >> Javascript

How can we use a JavaScript function as an object?


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>