• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JavaScript Prototype

April 10, 2014 //  by Krishna Srinivasan//  Leave a Comment

The prototype object is used to add property and methods to the objects. The prototype object can be used on any custom objects. Before creating a new object, prototype object has to be attached before the object name. Prototype is a global property which is present in almost all objects. The Object.prototype is used to create a new objects prototype where anyone can define a new property on it. Its goal is to add methods and set of properties to the object prototype property.

JavaScript Prototype Object  Syntax

Object.prototype.name= value

JavaScript Prototype Object  Example

<!DOCTYPE html>
<head>
<script type="text/javascript">
	function bike(company, name)
	{

		this.company = company;
		this.name = name;
	}
</script>
</head>
<body>
<script type="text/javascript">

	var mybike = new bike("Bajaj", "Pulsar");

	bike.prototype.price= null;
	mybike.price= 75000;

	document.write("Name of the company: " + mybike.company + "<br>");
	document.write("Name of the bike: " + mybike.name+ "<br>");
	document.write("Price of the bike: " + mybike.price);
</script>
</body>
</html>
  • In this example we have shown how prototype object is been used in this program.
  • We have defined function called bike by passing parameters and referring the current values by using “this” keyword and later we are creating custom object called “bike” using new keyword. We have used object Prototype to add price value in the program.
  • function bike(company, name) is used to define the unique function name and the parameter is been passed in function i.e. company and name.
  • this.company = company;
    this.name = name; is used to refer the current value by using “this” keyword.
  • var mybike = new bike(“Bajaj”, “Pulsar”); line contains variable name called “mybike” and created object called “bike” by using “new” keyword and we are passing the parameter values to that object.
  • bike.prototype.price= null;is used to add custom property to object.
  • mybike.price= 75000; is used to assign the value of the bike.

JavaScript Prototype Object Demo

  • Save the file as object_prototype.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser.

When the execution process is completed successfully we will get the following output:

JavaScript Prototype Object

Category: JavaScriptTag: JavaScript Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Number Rounding in JavaScript
Next Post: JavaScript InnerHTML »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact