JavaScript Objects Overview

Leave a Comment

JavaScript is an Object Oriented Programming (OOP) language. A programming language can be called object-oriented if it provides four basic capabilities to developers.

Encapsulation – the capability to store related information, whether data or methods, together in an object.

Aggregation – the capability to store one object inside of another object.

Inheritance – the capability of a class to rely upon another class (or number of classes) for some of its properties and methods.

Polymorphism – the capability to write one function or method that works in a variety of different ways.

Objects are composed of attributes. If an attribute contains a function, it is considered to be a method of the object otherwise, the attribute is considered a property.

Object Properties

Object properties can be any of the three primitive data types, or any of the abstract data types, such as another object. Object properties are usually variables that are used internally in the object’s methods, but can also be globally visible variables that are used throughout the page.

The syntax for adding a property to an object is:
objectName.objectProperty = propertyValue;

Example:
Following is a simple example to show how to get a document title using “title” property of document object:
Var str = document.title;

Object Methods:

The methods are functions that let the object do something or something be done to it. There is little difference between a function and a method, except that a function is a standalone unit of statements and a method is attached to an object and can be referenced by the this keyword.

Methods are useful for everything from displaying the contents of the object to the screen to performing complex mathematical operations on a group of local properties and parameters.

Example:
Following is a simple example to show how to use write() method of document object to write any content on the document.
Document.write(“This is text”);

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 new operator is followed by the constructor method.

In the following example, the constructor methods are Objects(), Array(), and Date().

These constructors are built-in JavaScript functions.
var employee = new Object();
var books = new Array(“C++”, “Peral”, “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 = "Peral";
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 this keyword 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){  //Create the object
this.subject = "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>


0 comments:

Post a Comment