Object literal
One of the most common approach for creating object in JavaScript is using the literals.So for example we can create an object of employee type using object literal as:
var employee= {name: 'Employee1', id:'1', address:'123 test-street' log:function() { console.log('employee: %s %s',name,address) } };
after you declare an object you can access any property or method of that object using the dot operator.To access the name property of the employee object you would use:
alert(employee.name);
to access the log method you would use:
employee.log();
In the log() method we are outputting the properties name and id.
Constructor function
You can declare JavaScript object using a constructor function.You define a function like a normal function passing the required parameters:
function Employee(name, id) { this.name = name; this.id = id; }
Now you can create an object using the new operator and Employee() function as:
var employee=new Employee('ABC',1');