0% found this document useful (0 votes)
13 views17 pages

JavaScript Objects: Creation & Methods

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)
13 views17 pages

JavaScript Objects: Creation & Methods

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

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@[Link],
in JS mobile: 9898987898 ,
login : function (){
[Link](“login success”)
}
};
@csworldtelugu
Ways to create object :
[Link] Literal

[Link] a new keyword with Object constructor

[Link] a new keyword with a constructor function


Objects [Link]() method
in JS
[Link]

@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. [Link]
movie = {
Ex: [Link]"
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. [Link] = value
in JS
Ex: [Link] =“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. [Link] = value
in JS
Ex: [Link] =“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 [Link]
in JS
Ex: delete [Link]

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


[Link] = name;
[Link] = age;
[Link] = place

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

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

@csworldtelugu
4. [Link] method:
let movie2 = [Link](movie);

let movie3 = [Link](movie, {


name: {
value: “RRR

Create object },
in JS Music: {
value: “Keeravani”
}
}) @csworldtelugu
5. Using ES6 Classes:
class definition :

Class user {
constructor(name, age,place) {
[Link] = name;
[Link] = age;
[Link] = place

Create object }
in JS }
Object creation :

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


@csworldtelugu
useful Object Methods:
• [Link]()

• [Link]()

• [Link]()

Objects • [Link]()
in JS
• [Link]()

@csworldtelugu
[Link]()

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 [Link]([Link](p1))

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

@csworldtelugu
[Link]()

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 [Link]([Link](p1))

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

@csworldtelugu
[Link]()
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 [Link]([Link](p1))

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

@csworldtelugu
[Link]()
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"
}

[Link](p1);
Objects [Link] = "12 mp";
[Link] = 30000;
in JS delete [Link]

[Link](p1)

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

@csworldtelugu
[Link]()
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"
}

[Link](p1);
Objects [Link] = "12 mp";
[Link] = 30000;
in JS delete [Link]

[Link](p1)

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

@csworldtelugu

You might also like