Javascript Objects

Javascript are capable to handle object based paradigm. This object is capable to store properties and its values. Generally properties is contain primitive values. But its possible to assign a method to that properties.

Creating Objects

In Javascript object can be created in a 4 different ways. such as using contain literals values, using Object constructor, using to create custom class constructor and using of Object Prototypes.

Using literals

//Create an object
let company = {
  name: "TI Company",
  employee: 10000,
  location:"India",
  status:"Active"
}
console.log(typeof company); //object
console.log(company);
object
{ name: 'TI Company',
  employee: 10000,
  location: 'India',
  status: 'Active' }

Object Constructor

Object constructor are create an object is defined using new Object();. And this constructor are returning a instance of object. This object constructor are used in two ways. Such as using without parameterized constructor are create an empty object.

let company =new Object();
//Add object properties
company.name ="TI Company";
company.employee =10000;
company.location ="India";
company.status ="Active";
console.log(typeof company);
console.log(company);
object
{ name: 'TI Company',
  employee: 10000,
  location: 'India',
  status: 'Active' }

And second way is to provide an initial value using parameterized constructor. See this example.

let company =new Object({
	name :"TI Company",
	employee :10000,
	location :"India",
	status :"Active",
});
//Add object properties

console.log(typeof company);
console.log(company);
object
{ name: 'TI Company',
  employee: 10000,
  location: 'India',
  status: 'Active' }

Class Constructor

class Company{
  constructor(){
    this.name ="TI this";
    this.employee =10000;
    this.location ="India";
    this.status ="Active";

  }
}
//Create object
let company =new Company();
console.log(typeof company);
console.log(company);
object
Company {
  name: 'TI this',
  employee: 10000,
  location: 'India',
  status: 'Active' }

Using Prototypes

Properties are variables which are store the information about an object. single object is capable to store multiple properties attributes. Let see an example.

let os ={
  name : "Ubuntu",
  version :"16.04"
}

let newVersion=Object.create(os);
console.log(os);

newVersion.version="19.04";
newVersion.features="Fast process";

console.log(newVersion);

console.log(os); 
Object prototype Example
{ name: 'Ubuntu', version: '16.04' }
{ version: '19.04', features: 'Fast process' }
{ name: 'Ubuntu', version: '16.04' }

In this example newVersion object is capable to use and accessing the name properties of os objects.

let os ={
  name : "Ubuntu",
  version :"16.04"
}

let newVersion=Object.create(os);
console.log(os);

newVersion.version="19.04";
newVersion.features="Fast process";
console.log(newVersion);
console.log(newVersion.name);
{ name: 'Ubuntu', version: '16.04' }
{ version: '19.04', features: 'Fast process' }
Ubuntu

Define method

In Javascript object is possible to binding a method to an instance variable. And this method are execute using name of object instance.

let info={

  name:"Image",
  width:"50px",
  height:"100px",
  about:function(){
    console.log(`${this.name}`);
    console.log(`${this.width}`);
    console.log(`${this.height}`);
  }
}

info.about();
Assigning method to an object instance variables
Image
50px
100px

In this example assigned simple method to object instance. In case method are accepted parameter values then passing that values in within the parenthesis.

Getter and Setter

Getter is very useful technique to perform special type of logical operation. For example.

var amount = {
  rupy:1000,
  get dollar() {
  	//75 in rupy
  	return this.rupy/75;
  },
  get euro() {
  	return this.rupy/79.32;
  }
};

console.log("Rupy : ",amount.rupy);
console.log("Dollar : ",amount.dollar);
console.log("Euro : ",amount.euro);
Rupy :  1000
Dollar :  13.333333333333334
Euro :  12.607160867372668

In this example rupee is a main properties and respective to this are work dollar and euro. When change rupee values then automatically other fields is modify.

Setter are used to set instance variable value of an objects

var student = {
  name:"Virat",
  age : 21,
  set newAge(age){
  	this.age=age;
  }
};
console.log(student);
//Set value using Setter
student.newAge=26;
//after update
console.log(student);
{ name: 'Virat', age: 21, newAge: [Setter] }
{ name: 'Virat', age: 26, newAge: [Setter] }


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved