What is an object in Javascript
Before creating an object in javascript, let’s understand what is an object in javascript and its behavior
- JavaScript has an object entity with a combination of properties.
- An object has the key-value pair.
- Every element has the name which is called key
- Objects help to group together different variable that belongs to the same person or entity.
- In object, the order doesn’t matter
- The object in javascript can be created using the keyword Object
- Every object in the javascript has properties associated with it.
- Properties of objects can be accessed with dot notation (.)
- You can only access the already assigned properties.
- If you try to access the properties those are not been assigned it will return undefined
Let’s consider below code snippet that creates an employee object with employee attributes
Create an employee object and assign properties:
//Employee object var Employee = new Object(); Employee.name = 'Name'; Employee.designation = 'CEO'; Employee.email = 'name@gmail.com';
Access property of employee object:
var Employee = new Object(); Employee.name = 'Name'; Employee.designation = 'CEO'; Employee.email = 'name@gmail.com'; console.log(Employee.name); //Output //Name
You can only access the already assigned properties. If you try to access the properties those are not been assigned it will return undefined
var Employee = new Object(); Employee.name = 'Name'; Employee.designation = 'CEO'; Employee.email = 'name@gmail.com'; console.log(Employee.address); //Output //undefined
Points to be considered while creating an object and accessing:
- Object property name should be a valid string or
- Object property name should be anything that can be converted to a string
- Object property can not start with a number and if you want to use the number or string with space as property name then use the below format
var Employee = new Object(); Employee.name = 'Name'; Employee.designation = 'CEO'; Employee.email = 'name@gmail.com'; Employee['address of residence'] = 'The complete address'; console.log(Employee['address of residence']); //Output //The complete address
How many ways can you create an object in javascript?
We can create an object in javascript in the below ways
1) With the help of object initializers: You can start creating an object by using the literal notation.
var Employee = { name:'Name', email:'name@gmail.com' } console.log(Employee.name); //Output //Name
2) With the help of constructor function: You can create an object with the new keyword with constructor
function Person(name, age, sex) { this.name = name; this.age = age; this.sex = sex; } var Person1 = new Person('Rajani', 33, 'M'); console.log(Person1.name); // output // Rajani
3) Using Object.create(): You can also create an object by using the method Object.create(). Object.create() method accepts the proto object which has to be inherited. This method is very useful when you want to inherit methods from the other objects.
var employee = { netPayable:function(){ return ' Employee salary details:\n Name: '+this.name+'\n Tax : '+this.tax+'\n Payable amount is : '+(this.salary - this.tax); } } // employee 1 var stark = Object.create(employee); stark.name = 'Stark'; stark.tax = 200; stark.salary = 20000; var starkPayableSalary = stark.netPayable(); console.log(starkPayableSalary); // employee 2 var mickel = Object.create(employee); mickel.name = 'Mickel'; mickel.tax = 400; mickel.salary = 30000; var mickelPayableSalary = mickel.netPayable(); console.log(mickelPayableSalary); // output // Employee salary details: // Name: Stark // Tax : 200 // Payable amount is : 19800 // Employee salary details: // Name: Mickel // Tax : 400 // Payable amount is : 29600
Also, read What is apply in javascript?
4 thoughts on “What is an object in Javascript”
Comments are closed.