Object in Javascript
Introduction
- JavaScript has an object entity with a combination of properties
- An object has the key-value pair
- Every element has a 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
//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
Accessing property
- 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
Key points
- 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
Ways create an object
- With the help of object initializers: You can start creating an object by using the literal notation
- With the help of the constructor function: You can create an object with the new keyword with the constructor
- Using Object.create()
// With the help of object initializers var Employee = { name: 'Name', email: 'name@gmail.com' } console.log(Employee.name); //Output //Name //With the help of constructor function // 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 // Using Object.create() // 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