Javascript Objects:

Introduction Properties Methods
Pre-defined objects Creating objects

JavaScript is an object-oriented scripting language.

What is an object ?
An object is a collection of properties (variables) and methods (functions) all brought under a single name. JavaScript lets you create your own objects for storing data and to use some predefined objects.

Eg: Every HTML document in the web has the following objects:

 

  • Navigator: has properties for the name and version of the Navigator being used
  • window: the top-level object; has properties that apply to the entire window.
  • document: contains properties based on the content of the document, such as title,background color, links, and forms.
  • location: has properties based on the current URL.
  • history: contains properties representing URLs the client has previously requested.


Top

 




Properties
You can access the properties of an object with notation: objectName.propertyName.

eg.   window.name

The document may contain other objects so to access the properties, you must specify the object name and all its ancestors. In the the following to access the value property of a text field named text1 in a form named yourform in the current document:

eg :  document.yourform.text1.value
Top




Javascript Objects – Methods
The object has several  methods which can be called using the basic syntax: objectName.methodName(). Any arguments required for the method are passed between the parentheses, just like a normal function call.

Eg: The window object has numerous useful methods, some of them are:

open and close: Opens and closes a browser window; you can specify the size of the window and  its content.
alert: Displays an Alert dialog box with a message.

confirm: Displays a Confirm dialog box with OK and Cancel buttons.

prompt: Displays a Prompt dialog box with a text field for entering a value.

blur and focus: Removes focus from, or gives focus to a window.

scrollTo: Scrolls a window to a specified coordinate.

setInterval: Evaluates an expression or calls a function each time the specified period elapses.

setTimeout: Evaluates an expression or calls a function once after the specified period elapses.

Top




Javascript – Creating Objects

You can create your own objects for storing data within a JavaScript program. There are several ways to create a new object.

Creating Objects: Instance of an object:

This means creating a new single object. For example consider a new object newemployee

newemployee=new Object();
newemployee.name=”Bob”;

newemployee.sex=”male”;

newemployee.country=”US”;

You can even assigning a method to a new object. Suppose you have a function named payroll() (which pays the employee), then :

newemployee.payroll=payroll;

Creating Objects: Prototype

This type of object means you are not creating an actual instance of the object, but rather you are defining the structure of the object so that later you can quickly create a particular instance of the object.

For example create a prototype object newemployee().

function newemployee(name, sex, country)
                    { this.name = name;

                      this.sex = sex;

                      this.country = country;

                    }

Now you can quickly create new objects based on the newemployee structure:
              employee1=new newemployee(“Lisa”,”female”,”US”);

              employee2=new newemployee(“Jerry”,”male”,”US”);

Top




Javascript – Predefined objects

JavaScript lets you use some predefined objects. The predefined core objects are Array, Boolean, Date, Function, Math, Number, RegExp, and String. A very good example is  the Array-object. What follows is a documentation for Array-object.

Top


 

Leave a Comment