0% found this document useful (0 votes)
4 views

Objects Notes

Object Creation in javascript Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Objects Notes

Object Creation in javascript Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Object:

• object is collection of elements in the form of properties

• Property is a key-value pair.

Ex: Var user1 = {


name:‘xyz',
Objects email: xyz@gmail.com,
in JS mobile: 9898987898 ,
login : function (){
console.log(“login success”)
}
};
@csworldtelugu
Ways to create object :
1.Object Literal

2.Using a new keyword with Object constructor

3.Using a new keyword with a constructor function


Objects 4.Object.create() method
in JS
5.Classes

@csworldtelugu
1. Using Object Literal :
let movie1 = { } -> Create an empty Object

let movie = { ->Object with properties (key: value pairs)

name: “RRR"

Create object release : 2021


in JS director : “Rajamouli"

@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

Ex: movie[“budget"] = “400 crores”

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

Ex: movie[“budget"] = “500 crores”

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:

let movie1= new Object();

movie1 .name = “RRR”;

movie1 .director = “Rajamouli”;


Create object
in JS
let movie1= new Object({
name:"RRR“,
budget:"200 cr"
});
@csworldtelugu
3. Using new Operator with Constructer function:
Step1: create constructor function :

function user(name, age,place) {


this.name = name;
this.age = age;
this.place = place

}
Create object
in JS Step2: create object with constructor function call :

Let user1= new user(“abc", 25,”Hyderabad”);

@csworldtelugu
4. Object.create method:
let movie2 = Object.create(movie);

let movie3 = Object.create(movie, {


name: {
value: “RRR

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 :

Let user1= new user(“abc", 25,”Hyderabad”);


@csworldtelugu
useful Object Methods:
• Object.keys()

• Object.values()

• Object.entries()

Objects • Object.freeze()
in JS
• Object.seal()

@csworldtelugu
Object.keys()

This method returns an array of all the keys of an object.

let p1 = {
name: "real me 8 pro",
price: 20000,
ram: "16 gb"
}

Objects
in JS console.log(Object.keys(p1))

o/p:[ 'name', 'price', 'ram' ]

@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))

o/p: [ 'real me 8 pro', 20000, '16 gb' ]

@csworldtelugu
Object.entries()
This method returns an array of arrays, where each inner array contains a

key-value pair of the object.

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)

o/p:{ name: 'real me 8 pro', price: 20000, ram: '16 gb' }

@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)

o/p:{ name: 'real me 8 pro', price: 30000, ram: '16 gb' }

@csworldtelugu

You might also like