Search within TutorialsPark

Javascript Objects

Javascript Objects are used to create Windows, forms , images etc.

What is Javascript Objects?

In Javascript , an object is defined as an " unordered collection of properties each of which contains a primitive value, object, or function "

The objects are described by properties and their behavior is defined by methods. An object is collection of these properties and methods which can be defined and altered and retrieved by the user.

Javascript objects are dynamic in nature, properties and methods can be added and deleted by the user.Each property or method is identified by the the name that is mapped to a value.

Since all Property and method names are strings, it can be said that objects map strings to values. The string-to-value maps can be called by various names like "hash" , "hashtable" , "dictionary", "associative array" .

Javascript Objects in Real Life.

Object Properties Methods
drones = "Reaper";

drone.length = "36 ft";

drone.weight = "2225 kg";

drone.payload = "1700 kg";




The properties of drone(object) describing it are, name , length , weight and payload .

The Actions drone can perform are defined by methods takeOff() , lockTarget() , fire() and returnBase() .

Javascript : Creating Objects

In Javscript, a custom objects can be created simply by creating a new instance of the Object and then add properties and methods to it.

The following example creates an object and then assigns properties and methods to it.

Syntax: Javascript Creating Object

Example: Javascript Creating Object

Give it a TRY! » Note: The object literals are a more preferred pattern over this method of creating objects

Javascript : Creating objects using the Literals

The more common way of creating objects is by using object literal notation. Its a comma seperated list of colon separated name:value pairs enclosed within curly braces

Syntax: Javascript Objects using Object literals

Example: Javascript Objects using - Object Literals

Give it a TRY! » Note: An object literal is an expression that creates and initializes a new and distinct object every time it is evaluated.

Javascript Objects : Properties

The attributes of the objects are defined by their properties , these properties describe the object.

To define a property, the object name is followed by the name of the property. The properties can be accessed only via their objects.

Syntax: Javascript Object properties

Example: Javascript Object Properties

Give it a TRY! » Note: The Object() constructor with keyword new is called to create an instance of the object called car.

Javascript Objects : Methods

Javascript method are used to define the behavior and functions of javascript objects.Methods are added to the object using the dot symbol, and can be accessed only via the objects.

The purpose of method is to perform some operation on the object.Eg the method calc can be used to calculate the distance travelled by a Car.

Syntax: Javascript Objects Methods

Example: Javascript Object Method

Give it a TRY! » Note: Methods are commands and behavior attached to a particular object only.