JavaBeat

  • Home
  • 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)
  • Privacy
  • Contact Us

Custom Objects in JavaScript

April 11, 2014 by Krishna Srinivasan Leave a Comment

Custom objects are user-defined object where user can create its own object and it behaves exactly how the user wants it. We can create object by using the following steps:

  • We can define an object by using an object function.
  • Later on, instantiate new object by using “new” keyword.

JavaScript Custom Object Syntax

[code lang=”html”]
function userobject(parameter)
{
}
[/code]

Where,

  • userobject: It is the unique name of the function.
  • parameter: It is used to pass parameter in the function.

JavaScript Custom Object Example

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

bike.company = company;
bike.name = name;
}

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

document.write("Name of the company: " + bike.company + "<br>");
document.write("Name of the bike: " + bike.name);
</script>
</head>
<body>

</body>
</html>
[/code]

  • In the above program we have created custom objects that is user-defined object.
  • We have created our own object called bike which is unique name of the function and we have passed the parameters company and name.
  • 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.
  • document.write(“Name of the company: ” + bike.company + “<br>”); is used to display the company name and bike name in the output.

JavaScript Custom Object Demo

  • Save the file as customObject_example.html in eclipse IDE.
  • Select the file and right click on the file.

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

JavaScript Custom Objects Demo

Filed Under: JavaScript Tagged With: 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.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved