Objects and it’s internal representation in JavaScript.

Objects overview

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.

Objects and properties

A JavaScript object has properties associated with it. A property of an object can be explained as a variable that is attached to the object. Object properties are basically the same as ordinary JavaScript variables, except for the attachment to objects. The properties of an object define the characteristics of the object. You access the properties of an object with a simple dot-notation:

Like all JavaScript variables, both the object name (which could be a normal variable) and property name are case sensitive. You can define a property by assigning it a value. For example, let’s create an object named and give it properties named , , and as follows:

Unassigned properties of an object are (and not ).

Properties of JavaScript objects can also be accessed or set using a bracket notation (for more details see property accessors). Objects are sometimes called associative arrays, since each property is associated with a string value that can be used to access it. So, for example, you could access the properties of the object as follows:

Object Creation

1. Using object initializers :

Using object initializers is sometimes referred to as creating objects with literal notation. The above example could also be written using an object initializer, which is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ():

The syntax for an object using an object initializer is:

where is the name of the new object, each is an identifier (either a name, a number, or a string literal), and each is an expression whose value is assigned to the . The and assignment is optional; if you do not need to refer to this object elsewhere, you do not need to assign it to a variable.

2. Using a constructor function :

Alternatively, you can create an object with these two steps:

  1. Define the object type by writing a constructor function. There is a strong convention, with good reason, to use a capital initial letter.
  2. Create an instance of the object with .

To define an object type, create a function for the object type that specifies its name, properties, and methods. For example, suppose you want to create an object type for cars. You want this type of object to be called , and you want it to have properties for make, model, and year. To do this, you would write the following function:

Notice the use of to assign values to the object's properties based on the values passed to the function.

Now you can create an object called as follows:

This statement creates and assigns it the specified values for its properties. Then the value of is the string "Eagle", is the integer 1993, and so on.

You can create any number of objects by calls to . For example,

3.Using the method :

Objects can also be created using the method. This method can be very useful, because it allows you to choose the prototype object for the object you want to create, without having to define a constructor function.



In the above example, the object obj contains property called name & the newly object obj2 created will inherit all the prototype object properties of obj.

Thank You!