Objects Notes
Objects Notes
@csworldtelugu
1. Using Object Literal :
let movie1 = { } -> Create an empty Object
name: “RRR"
@csworldtelugu
How to access object values :
We can access values by using keys in 2 ways
1. object-name.key
movie = {
Ex: movie.name"
name: “RRR"
release : 2021
Objects
in JS 2. object-name[''key'']
director : “Rajamouli"
}
Ex: movie[''name‘’]
@csworldtelugu
How to add new properties to object:
We can add properties by using keys in 2 ways
1. object-name["key"] = value
Create object
2. object-name.key = value
in JS
Ex: movie.budget =“400 crores”
@csworldtelugu
How to update values of object :
We can update values by using keys in 2 ways
1. obj["key"] = value
Create object
2. obj.key = value
in JS
Ex: movie.budget =“500 crores”
@csworldtelugu
How to delete values of object :
We can delete values by using keys in 2 ways
1. delete obj["key"]
delete movie[“budget"]
Create object
2. delete obj.key
in JS
Ex: delete movie.budget
@csworldtelugu
2. Using new Operator Object Constructer:
}
Create object
in JS Step2: create object with constructor function call :
@csworldtelugu
4. Object.create method:
let movie2 = Object.create(movie);
Create object },
in JS Music: {
value: “Keeravani”
}
}) @csworldtelugu
5. Using ES6 Classes:
class definition :
Class user {
constructor(name, age,place) {
this.name = name;
this.age = age;
this.place = place
Create object }
in JS }
Object creation :
• Object.values()
• Object.entries()
Objects • Object.freeze()
in JS
• Object.seal()
@csworldtelugu
Object.keys()
let p1 = {
name: "real me 8 pro",
price: 20000,
ram: "16 gb"
}
Objects
in JS console.log(Object.keys(p1))
@csworldtelugu
Object.values()
This method returns an array of all the values associated with each key
of an object.
let p1 = {
name: "real me 8 pro",
price: 20000,
ram: "16 gb"
}
Objects
in JS console.log(Object.values(p1))
@csworldtelugu
Object.entries()
This method returns an array of arrays, where each inner array contains a
let p1 = {
name: "real me 8 pro",
price: 20000,
ram: "16 gb"
}
Objects
in JS console.log(Object.entries(p1))
o/p:[[ 'name','real me 8 pro' ],[ 'price', 20000 ],[ 'ram', '16 gb' ]]
@csworldtelugu
Object.freeze()
This method prevents any changes to the object's properties. so that we
can't add, delete, or modify any properties of the object after this line
let p1 = {
name: "real me 8 pro",
price: 20000,
ram: "16 gb"
}
Object.freeze(p1);
Objects p1.camera = "12 mp";
p1.price = 30000;
in JS delete p1.ram
console.log(p1)
@csworldtelugu
Object.seal()
This method prevent any additions or deletions of properties to the
object. But we modify the values of the existing properties of the object.
let p1 = {
name: "real me 8 pro",
price: 20000,
ram: "16 gb"
}
Object.freeze(p1);
Objects p1.camera = "12 mp";
p1.price = 30000;
in JS delete p1.ram
console.log(p1)
@csworldtelugu