JavaScript Tutorial

adplus-dvertising
Object prototypes
Previous Home Next

javascript Prototypes allow you to easily define methods to all instances of a particular object. Prototyping also provides the facility to add, replace, or delete methods

Syntax :

object.prototype.name = value

Example :

<html>
<head>
<script>
function book(title, author){
    this.title = title;
    this.author  = author;
}
</script>

</head>

<body>
<script>
   
   var myBook = new book("java", "prasun");
   book.prototype.price = null;
   myBook.price = 500;
   document.write("Book title is : " + myBook.title + "<br>");
   document.write("Book author is : " + myBook.author + "<br>");
   document.write("Book price is : " + myBook.price + "<br>");
</script>
</body>
</html>

Output :

Book title is : java
Book author is : prasun
Book price is : 500
Previous Home Next