Tutorials Point

  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint

  Home     References     Discussion Forums     About TP  

Javascript Object - prototype

previous next AddThis Social Bookmark Button



The prototype property allows you to add properties and methods to any object (Number, Boolean, String and Date etc).

Note: Prototype is a global property which is available with almost all the objects.


object.prototype.name = value


Here is an example showing how to use the prototype property to add a property to an object:

<title>User-defined objects</title>

<script type="text/javascript">

function book(title, author){
    this.title = title; 
    this.author  = author;
<script type="text/javascript">
   var myBook = new book("Perl", "Mohtashim");
   book.prototype.price = null;
   myBook.price = 100;
   document.write("Book title is : " + myBook.title + "<br>");
   document.write("Book author is : " + myBook.author + "<br>");
   document.write("Book price is : " + myBook.price + "<br>");

This will produce following result:

Book title is : Perl
Book author is : Mohtashim
Book price is : 100

To understand it in better way you can Try it yourself.

previous next Printer Friendly