An object is a grouping of one or more pieces of data and one or more functions into a named package. The functions, also known as methods, generally operate on the data inside the object. The data inside an object are often referred to as the 'properties' of the object. This process of joining data and methods is known as 'encapsulation' and has a long history of development through 'object-oriented' languages such as Smalltalk, Eiffel and, in recent years, Java and C++. JavaScript is not a full implementation of an OOP language but it has enough to provide a useful introduction. Browsers, along with many other major software items, are themselves collections of objects which can be accessed and programmed through JavaScript - you have already seen some of these in action such as the Math object, the Date object, the String object and the Image object.
An object is declared as follows:
var Person = new Object; Person.name = 'John Doe'; Person.Dob = new Object; Person.Dob.day = 23; Person.Dob.month = 5; Person.Dob.year = 1965; var Boss = new Person delete Person
The key word new creates the object and properties are assigned to it in the form:
object_variable_name.property_name = value;
Object declarations can contain other objects (nested) in which case the dot notation is stretched to include both objects. Objects can be cleared out of memory with delete and they will also be destroyed when a function call finishes if they were created inside that function.
Objects are distinct from variables in that a variable has a data type and a value associated with it but an object variable does not. An object variable points to the collection of properties and methods contained in the object and they are accessed through the object name. A variable can be passed to a function as an argument but an object cannot. Objects in JavaScript are very similar to arrays and it is a general property of arrays that they are not passed as parameters to functions because they are potentially very large and moving them around would consume too much of a computer's resources. While variables are typically passed to a function by value arrays and objects are passed by reference or address. This means that a function does not operate on a copy of an array or object created by the function call but on the array or object itself. Similarly with objects, they are passed to functions by reference and no copy is made by the function. Objects are potentially large and it would be wasteful of resources to make copies.
The methods of an object lie inside it and act upon its data. When an object's method is called it can use the this variable which points to the object currently in scope, that is whichever Object has called the function. The this variable has access to all the properties and methods of the object, although methods which use this are not plain functions anymore. Thus this can be used to include object properties in any member function (a method of the object).
function say_hello(){alert("Hello");}
function say_with_this()
{alert("hello " + this.name);} //this variable processes the current object properties
var Person=new Object;
Person.name='John Doe';
Person.hello=say_hello(); //properties with function calls are methods
Person.hello_name=say_with_this();
Person.hello(); //call the methods
Person.hello_name();
The function say_hello can be called from outside an object but the function say_with_this cannot because it includes the object-related word this. The function say_with_this can only be called from inside an object.
A variable of type integer or string is known as a primitive data type which can contain only one variable at a time. An object, on the other hand, is more like a bag which can contain as many properties as the programmer wishes. An object, however, is defined as a variable with the new statement, so what is going on? A variable of type object (as in var Person=new Object) is not the data of the object itself but rather a pointer to the bag of data which is the object it refers to. This explains why object notation is of the form object.property, where the object name is the pointer to the bag of data and the property is the data item inside the bag.
One consequence of this is that more than one variable can refer to the same object, as in var x,y,z = new Object.
Another consequence of the pointer status of object variables is that Objects can have prototypes and constructors which can make the task of defining objects easier. A prototype can set the properties and methods within an object to common values such as an address which all employees share or an organisation to which they all belong like a pension fund. Here is an example:
function show(){
Return to Javascript Menu