Javascript objects
Th object type represents on of the javascript datatypes. It is used to store various key value pairs and much more complex entities.
Objects can be created using the object(), constructer or the object initializer/literal syntax. In javascript almost everything is a object.
Boolean, number, string can also be an object if defined with new keyword.
Dates, maths, regularexpressions, array , functions amd objects are always objects.
IMP: All javascript values except primitives, are objects.
Primitive value: A primitive value is a value that has no properties or methods of itself.
JS has 7 types of primitive values
String
Number
Boolean
Null
undefined
Symbol
bigint
Object methods: Methods are the actions that can be performed on objects. Object properties can be both primitive values other objects or any function.
Creating a javascript object:
By using and object literal.
By using the new keyword.
Define and object constructor, and then create object of the constructed type.
Creating object using
object.create
.
1. By using and object literal.
const info={
name:"Abhay",
age:"22",
address:"noida"
}
2. By using the new keyword.
const info= new object{};
info.name:"Abhay",
info.age:"22",
info.address:"noida"
3. Define and object constructor, and then create object of the constructed type.
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
4. Creating object using object.create
.
The Object.create() static method creates a new object, using an existing object as the prototype of the newly created object.
Object.create(prototype[, propertiesObject])
Parameters:
prototype: It is the prototype object from which a new object has to be created.
propertiesObject: It is an optional parameter. It specifies the enumerable properties to be added to the newly created object.
const person = {
isHuman: false,
printIntroduction: function() {
console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
}
};
const me = Object.create(person);
me.name = 'Matthew'; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // Inherited properties can be overwritten
me.printIntroduction();
// Expected output: "My name is Matthew. Am I human? true"
Javascript object properties :
Accessing javascript object properties:
const info={
name:"Abhay",
age:"22",
address:"noida"
}
info.name;
//expected output Abhay
info.age;
//expected output 22
or
const info={
name:"Abhay",
age:"22",
address:"noida"
}
info["name"];
//expected output Abhay
info["age"];
//expected output 22
How to iterate through objects:
const info={
name:"Abhay",
age:"22",
address:"noida"
}
for(let x in info){
console.log(info[x]);
}
How to add new properties to object: We can simply add new properties to an existing object by simply assigning it a value.
const info={
name:"Abhay",
age:"22",
address:"noida"
}
info.number="1234";
//1234 will be added to info object as a key value pair
How to delete properties of an object: We can simply delete properties of an existing object by simply using Delete keyword.
const info={
name:"Abhay",
age:"22",
address:"noida"
}
Delete info.age;
//age property will get deleted from the info object
Nested objects: Multiple objects can be nested inside the each other in javascript.
const info={
name:"Abhay",
age:"22",
address:"noida"
bikes:{
redbike:"harley",
blackbike:"suzuki"
}
}
console.log(info.bikes.redbike);
//Expected output: harley