How to Create an Object

You can create an object like this:

var objName = new Object(); objName.property1 = value1; objName.property2 = value2; objName.method1 = function() { line of code }

OR

var objName= {property1:value1, property2:value2, method1: function()

{ lines of code} };

Access Object Properties and Methods You can access properties of an object like this:

objectname.propertyname;

You can access methods of an object like this:

objectname.methodname();

Try this Example yourself:

Objects!!!

OOPS Constructor

But creating objects of this kind is not that useful because here also, you will have to create different objects for different students. Here comes object constructor into picture. Object constructor helps you create an object type which can be reused to meet the need of individual instance. Try this Example yourself:

Loop Through the Properties of an Object

Syntax:

for (variablename in objectname)

{

lines of code to be executed

}

The for/in a loop is usually used to loop through the properties of an object. You can give any name for the variable, but the name of the object should be the same as that of an already existing object which you need to loop through. Try this Example yourself: