Posted By:
26 Aug, 2014 12:17 pm PDT

User defined Objects in JavaScript

Can we create the objects by ourselves.?

Attachment:None


User-Defined Objects:

All user-defined objects and built-in objects are descendants of an object called Object.

The new Operator:

The new operator is used to create an instance of an object. To create an object, the newoperator is followed by the constructor method.

In the following example, the constructor methods are Object(), Array(), and Date(). These constructors are built-in JavaScript functions.

var employee = new Object();

var books = new Array("C++", "Perl", "Java");

var day = new Date("August 15, 1947");

 

The Object() Constructor:

A constructor is a function that creates and initializes an object. JavaScript provides a special constructor function called Object() to build the object. The return value of the Object() constructor is assigned to a variable.

The variable contains a reference to the new object. The properties assigned to the object are not variables and are not defined with the var keyword.

Example 1:

This example demonstrates how to create an object:

<html>

<head>

<title>User-defined objects</title>

<script type="text/javascript">

var book = new Object();   // Create the object

    book.subject = "Perl"; // Assign properties to the object

    book.author  = "Mohtashim";

</script>

</head>

<body>

<script type="text/javascript">

   document.write("Book name is : " + book.subject + "<br>");

   document.write("Book author is : " + book.author + "<br>");

</script>

</body>

</html>

 

Example 2:

This example demonstrates how to create an object with a User-Defined Function. Here thiskeyword is used to refer to the object that has been passed to a function:

<html>

<head>

<title>User-defined objects</title>

<script type="text/javascript">

function book(title, author){

    this.title = title;

    this.author  = author;

}

</script>

</head>

<body>

<script type="text/javascript">

   var myBook = new book("Perl", "Mohtashim");

   document.write("Book title is : " + myBook.title + "<br>");

   document.write("Book author is : " + myBook.author + "<br>");

</script>

</body>

</html>

 

Defining Methods for an Object:

The previous examples demonstrate how the constructor creates the object and assigns properties. But we need to complete the definition of an object by assigning methods to it.

Example:

Here is a simple example to show how to add a function along with an object:

<html>

<head>

<title>User-defined objects</title>

<script type="text/javascript">

 

// Define a function which will work as a method

function addPrice(amount){

    this.price = amount;

}

 

function book(title, author){

    this.title = title;

    this.author  = author;

    this.addPrice = addPrice; // Assign that method as property.

}

 

</script>

</head>

<body>

<script type="text/javascript">

   var myBook = new book("Perl", "Mohtashim");

   myBook.addPrice(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>");

</script>

</body>

</html>

 
Was it useful?  




Please login to reply to this problem.