Home | | Web Technology | JavaScript Objects

Chapter: Web Technology : JavaScript (JS)

JavaScript Objects

JavaScript Objects represent self contained entities consisting of variables (called properties in object terminology) and functions (called methods) that can be used to perform tasks and store complex data.

OBJECTS

 

JavaScript Objects represent self contained entities consisting of variables (called properties in object terminology) and functions (called methods) that can be used to perform tasks and store complex data. JavaScript objects fall into three categories: Built-in Objects, Custom Objects and Document Object Model (DOM) Objects. Built-in objects are objects that are provided with JavaScript to make your life as a JavaScript developer easier. In many of the examples given in this book we have used the document.write() mechanism to write text to the current web page. Whether you knew it or not, you have been using the write() method of the JavaScript built-in document object when you have run these scripts. Document Object Model (DOM) Objects provide the foundation for creating dynamic web pages. The DOM provides the ability for a JavaScript script to access, manipulate, and extend the content of a web page dynamically (i.e. without having to reload the page). The DOM essentially presents the web page as a tree hierarchy of objects representing the contents and elements of the web page. These objects, in turn, contain properties and methods that allow you to access and change parts of the web page. Custom objects are objects that you, as a JavaScript developer, create and use.

 

Creating a Custom JavaScript Object

 

Creating a custom JavaScript object is quite similar to constructing a function. The syntax is as follows:

 

function object(''parameter1, parameter2, parameter3,...'')

 

{

 

this.property1 = parameter1;

 

this.property2 = parameter2; this.property3 = parameter3; this.method1

 

= function1; this.method2

 

= function2; this.method3

 

= function3;

}

 

In the above outline object refers to the name of the object - this can be any valid (and hopefully descriptive) name you choose to use. The parameters define the values that you will pass into the object when you instantiate it later.

 

Creating and Using Object Instances

 

In the previous section we learned how to create an object definition. It is important to note that, at this point, we have only described what the object will do (we have basically created blueprint of the object), we have not actually created an object we can work with (this is known as an object instance). Object instances are created using the new keyword and are assigned to an object variable that will be used to reference the object. For example, in the following script we will create a new instance of the car object with the name myCar:

 

carObject = new car ("Ford", "Focus", "Red");

 

 

 

Extending Objects

 

JavaScript object instances are extremely flexible in that they can easily be extended. To extend an object that you have already created use object prototypes. The syntax for prototyping is as follows: objectType.prototype.propertyName.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web Technology : JavaScript (JS) : JavaScript Objects |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.