In Java Script Objects are very useful to organise the data or information. An object is a collection of properties. These properties can either be primitive data types, other objects, or functions.
In JavaScript we may create our own objects for storing data. More commonly, though, we will use the many "built-in" objects which allow us to work with, manipulate, and access the Web page and Web browser.
Creating Object In JavaScript
We can create an object of class myobject from the following function.
Example:1
function myobject()
{
this.containValue = 0;
this.othercontainValue = 0;
this.anothercontainValue = 0;
}
var mything = new myobject();
Here mything is an instance of class myobject. It will have the following properties, all of which will be assign to 0:
mything.containValue
mything.othercontainValue
mything.anothercontainValue
We can also assign the value to myobject.prototype.newContainValue = someValue; and all instances of class myobject will have the property newContainValue with value someValue.
Writing Constructors
You also create the Object by using cunstructors.To write your own constructors, you use the this keyword within the constructor to refer to the newly-created object. The constructor initializes the object.
The constructor in the example starts at an index of 0, this is not required. We can start with a first index of 1 if, for example, We want a parameter that indicates the actual number of indexes of the array or object. In the example, it's called extent to distinguish it from the automatically maintained length parameter of the built-in Array( ) object). If we write code that adds properties to the array, you have to update the extent parameter (or your equivalent) because this parameter is not maintained by JScript. Notice that even this extremely simple example uses both object (dot) and array (bracket) notation styles to refer to the current object.
function MakeStringArray(length)
{
this.extent = length;
for (iNum = 0; iNum < length; i++)
{
this[iNum] = "";
}
}
// Use the constructor to create and initialize an array.
myStringArray = new MakeStringArray(63);
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.