SlideShare a Scribd company logo
13_User_Defined_Objects.pptx objects in javascript
USER DEFINED
OBJECTS IN JAVASCRIPT
Objects In JavaScript
Before learning about objects , we must first understand a very
important statement which is very commonly used for
JavaScript
Javascript is not a class based language but a prototype based language.
What Is Class Based Language?
A class-based language is based on two fundamental entities:
“classes” and “instances”.
Class:
• A class is a user-defined data type.
• It describes a family of objects that have the same set of methods and
properties.
• For example, the Employee class could represent the set of all employees.
• Languages like Java, C++,Python etc are all class based languages
What Is Class Based Language?
Instance:
• An instance, is the instantiation of a class.
• For example, Sachin could be an instance of the Employee class, representing
a particular individual as an employee
• An instance has exactly the same properties as its class (no more, no less).
What Is Prototype Based
Language?
• A prototype-based language, such as JavaScript, does not make this
distinction: it simply has objects.
• In simple words we can say that creating a class is not required in
prototype based languages before creating an object.
• A prototype-based language has the notion of a prototypical object
What Is Prototype Based
Language?
• A prototypical object is used as a template from which we get the
initial properties for a new object.
• And in JavaScript this prototypical object is called Object which
provides all the basic properties and methods like toString() , valueOf()
to new objects
• Then these new objects can further specify their own properties, either
when we create them or at run time.
Objects In JavaScript
In JavaScript an object is a non-primitive data type.
It is like any other variable, the only difference is that an object
holds data in terms of properties and actions in terms of
methods.
An Important Point !
In other programming languages like Java or C#, we need a class
to create an object of it.
In JavaScript, an object is a standalone entity because designing
class is not compulsory to create objects in JavaScript.
However from ES6 onwards we can create a class if we want that.
Creating Objects
In JavaScript, an object can be created in two ways:
Using Object
Literal
Using Object
Constructor
Object Constructor
The first way to create an object is with Object Constructor using
the new keyword.
Syntax:
let <obj_name>=new Object();
Example:
let myCar=new Object();
Adding Properties
Properties are data members of an object used to hold values .
We can attach properties to an object in 2 ways:
1. Using the dot notation.
2. Using [ ] brackets and specifying property name as string.
Adding Properties Using Dot Operator
1. Using the dot notation.
Syntax:
<obj_name>.<property_name>=value;
Example:
myCar.name=“Baleno”;
myCar.year=2018;
Adding Properties Using [ ] Operator
2. Using the [ ] operator.
Syntax:
<obj_name>[“<property_name>”]=value;
Example:
myCar[“name”]=“Baleno”;
myCar[“year”]=2018;
Which To Prefer When ?
As a beginner we might get confused between when to use dot
notation and when to use [ ].
The answer is very simple :
Always use dot notation as its is the best and recommended way
But there are 2 cases when we will have to use [ ] and they are:
1. When property name contains spaces.
2. When property name is stored in a variable.
Special Point !
When a property name contains spaces, we need to create it
using [ ]
and to access it also we have to use [ ] .
For example:
myCar[“reg no”]=“MP04CB2314”;
console.log(myCar[“reg no”]);
If we use dot operator it will be an error
Special Point !
When a property name is stored in a variable, we need to create as
well
as access it we need use [ ]
For example:
var str=“company”;
console.log(myCar[str]); // will show Maruti
console.log(myCar.str); // will show
undefined
Special Point !
Reading from a property that does not exist will result in
an undefined.
For example:
console.log(myCar.price); // undefined
Changing Property Value
To change the value of a property, we simply use the
assignment
operator
Example:
myCar[“name”]=“Baleno”;
myCar[“year”]=2018;
myCar[“name”]=“Ciaz”;
console.log(myCar.name);// Ciaz
console.log(myCar.year);// 2018
Deleting A Property
To delete a property from an object, we use the delete operator:
Example:
myCar[“name”]=“Baleno”;
myCar[“year”]=2018;
delete myCar.year;
console.log(myCar.name);// Baleno
console.log(myCar.year);// undefined
Checking Whether A Property Exists Or Not
To check if a property does exists in an object, we use a special
operator called the in operator:
Example:
myCar[“name”]=“Baleno”;
myCar[“year”]=2018;
delete myCar.year;
console.log(‘name’ in myCar);// true
console.log(‘year’ in myCar);// false
Iterating Over Properties
To iterate over all properties of an object without knowing
property
names, we use the for...in loop:
Syntax:
for (key in object) {
// executes the body for each key among object properties
}
Example:
myCar[“name”]=“Baleno”;
myCar[“year”]=2018;
for (let key in myCar)
Adding Methods
Methods can also be added to the object
A method is nothing but a function that will access/manipulate
object’s properties.
Methods can be part of object during creation or can be added
later
like properties
Adding Methods
Syntax:
<obj_name>[“<func_name>”]=function(){
// body
};
Example:
myCar[“start”]=function(){
console.log(“starting the car”);
};
myCar.start(); // starting the car
Adding Methods
Syntax:
<obj_name>.func_name>=function(){
// body
};
Example:
myCar.start=function(){
console.log(“starting the car”);
};
myCar.start(); // starting the car
Nested Objects
We can assign another object as a property of an object.
Syntax:
<obj_name>.<obj_name>=new Object();
<obj_name>.<obj_name>.<property_name>=<value>;
Example:
myCar.stereo=new Object();
myCar.stereo.name=“Sony";
PROGRAM
Write a JavaScript code to access all the properties of
myCar objects and it’s nested object using for in loop
Creating Object Using Object Literal
The object literal is a simple way of creating an object and it is
done using { } brackets.
We can include key-value pair in { }, where key would be property
or method name and value will be value of property or definition
of the method.
We use comma (,) to separate multiple key-value pairs.
Creating Object Using Object Literal
Syntax:
let <obj_name>={
<prop_name>:<value>,
<prop_name>:<value>
};
Example:
let myCar={
company:”Maruti”,
year:2014
Adding Method Using Object Literal
Syntax:
let <obj_name>={ <func_name>:function(){
// body
}
};
Example:
let myCar={start:function(){
console.log(“starting the car”);
}
};
PROGRAM
Given the following object , write a for in loop to
calculate and print total marks
let student = { name: "Sumit“,age: 15, phy: 45,chem: 5
0,
maths: 65
};
Ad

Recommended

Prototype Object.pptx
Prototype Object.pptx
Steins18
 
Intro to iOS Development • Made by Many
Intro to iOS Development • Made by Many
kenatmxm
 
Constructors In Java – Unveiling Object Creation
Constructors In Java – Unveiling Object Creation
Geekster
 
COMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptx
FarooqTariq8
 
22519 - Client-Side Scripting Language (CSS) chapter 1 notes .pdf
22519 - Client-Side Scripting Language (CSS) chapter 1 notes .pdf
sharvaridhokte
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
Sopheak Sem
 
Class methods
Class methods
NainaKhan29
 
creating objects and Class methods
creating objects and Class methods
NainaKhan28
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
Jalpesh Vasa
 
IWT presentation121232112122222225556+556.pptx
IWT presentation121232112122222225556+556.pptx
dgfs55437
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
Vu Tran Lam
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Android App code starter
Android App code starter
FatimaYousif11
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
Hossam Ghareeb
 
QTP Descriptive programming Tutorial
QTP Descriptive programming Tutorial
Jim Orlando
 
Objective c
Objective c
ricky_chatur2005
 
object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
Design patterns in javascript
Design patterns in javascript
Ayush Sharma
 
OOP and C++Classes
OOP and C++Classes
MuhammadHuzaifa981023
 
Lecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentation
GomathiUdai
 
Lecture 9
Lecture 9
Mohammed Saleh
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
Saurabh Narula
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2
Rakesh Madugula
 
Lecture 4. mte 407
Lecture 4. mte 407
rumanatasnim415
 
Oop java
Oop java
Minal Maniar
 
Intro to Objective C
Intro to Objective C
Ashiq Uz Zoha
 
New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
Hoat Le
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 

More Related Content

Similar to 13_User_Defined_Objects.pptx objects in javascript (20)

Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
Jalpesh Vasa
 
IWT presentation121232112122222225556+556.pptx
IWT presentation121232112122222225556+556.pptx
dgfs55437
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
Vu Tran Lam
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Android App code starter
Android App code starter
FatimaYousif11
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
Hossam Ghareeb
 
QTP Descriptive programming Tutorial
QTP Descriptive programming Tutorial
Jim Orlando
 
Objective c
Objective c
ricky_chatur2005
 
object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
Design patterns in javascript
Design patterns in javascript
Ayush Sharma
 
OOP and C++Classes
OOP and C++Classes
MuhammadHuzaifa981023
 
Lecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentation
GomathiUdai
 
Lecture 9
Lecture 9
Mohammed Saleh
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
Saurabh Narula
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2
Rakesh Madugula
 
Lecture 4. mte 407
Lecture 4. mte 407
rumanatasnim415
 
Oop java
Oop java
Minal Maniar
 
Intro to Objective C
Intro to Objective C
Ashiq Uz Zoha
 
New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
Hoat Le
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
Jalpesh Vasa
 
IWT presentation121232112122222225556+556.pptx
IWT presentation121232112122222225556+556.pptx
dgfs55437
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
Vu Tran Lam
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Android App code starter
Android App code starter
FatimaYousif11
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
Hossam Ghareeb
 
QTP Descriptive programming Tutorial
QTP Descriptive programming Tutorial
Jim Orlando
 
object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
Design patterns in javascript
Design patterns in javascript
Ayush Sharma
 
Lecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentation
GomathiUdai
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
Saurabh Narula
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2
Rakesh Madugula
 
Intro to Objective C
Intro to Objective C
Ashiq Uz Zoha
 
New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
Hoat Le
 

Recently uploaded (20)

Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
LDMMIA Shop & Student News Summer Solstice 25
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
LDMMIA Shop & Student News Summer Solstice 25
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
Ad

13_User_Defined_Objects.pptx objects in javascript

  • 3. Objects In JavaScript Before learning about objects , we must first understand a very important statement which is very commonly used for JavaScript Javascript is not a class based language but a prototype based language.
  • 4. What Is Class Based Language? A class-based language is based on two fundamental entities: “classes” and “instances”. Class: • A class is a user-defined data type. • It describes a family of objects that have the same set of methods and properties. • For example, the Employee class could represent the set of all employees. • Languages like Java, C++,Python etc are all class based languages
  • 5. What Is Class Based Language? Instance: • An instance, is the instantiation of a class. • For example, Sachin could be an instance of the Employee class, representing a particular individual as an employee • An instance has exactly the same properties as its class (no more, no less).
  • 6. What Is Prototype Based Language? • A prototype-based language, such as JavaScript, does not make this distinction: it simply has objects. • In simple words we can say that creating a class is not required in prototype based languages before creating an object. • A prototype-based language has the notion of a prototypical object
  • 7. What Is Prototype Based Language? • A prototypical object is used as a template from which we get the initial properties for a new object. • And in JavaScript this prototypical object is called Object which provides all the basic properties and methods like toString() , valueOf() to new objects • Then these new objects can further specify their own properties, either when we create them or at run time.
  • 8. Objects In JavaScript In JavaScript an object is a non-primitive data type. It is like any other variable, the only difference is that an object holds data in terms of properties and actions in terms of methods.
  • 9. An Important Point ! In other programming languages like Java or C#, we need a class to create an object of it. In JavaScript, an object is a standalone entity because designing class is not compulsory to create objects in JavaScript. However from ES6 onwards we can create a class if we want that.
  • 10. Creating Objects In JavaScript, an object can be created in two ways: Using Object Literal Using Object Constructor
  • 11. Object Constructor The first way to create an object is with Object Constructor using the new keyword. Syntax: let <obj_name>=new Object(); Example: let myCar=new Object();
  • 12. Adding Properties Properties are data members of an object used to hold values . We can attach properties to an object in 2 ways: 1. Using the dot notation. 2. Using [ ] brackets and specifying property name as string.
  • 13. Adding Properties Using Dot Operator 1. Using the dot notation. Syntax: <obj_name>.<property_name>=value; Example: myCar.name=“Baleno”; myCar.year=2018;
  • 14. Adding Properties Using [ ] Operator 2. Using the [ ] operator. Syntax: <obj_name>[“<property_name>”]=value; Example: myCar[“name”]=“Baleno”; myCar[“year”]=2018;
  • 15. Which To Prefer When ? As a beginner we might get confused between when to use dot notation and when to use [ ]. The answer is very simple : Always use dot notation as its is the best and recommended way But there are 2 cases when we will have to use [ ] and they are: 1. When property name contains spaces. 2. When property name is stored in a variable.
  • 16. Special Point ! When a property name contains spaces, we need to create it using [ ] and to access it also we have to use [ ] . For example: myCar[“reg no”]=“MP04CB2314”; console.log(myCar[“reg no”]); If we use dot operator it will be an error
  • 17. Special Point ! When a property name is stored in a variable, we need to create as well as access it we need use [ ] For example: var str=“company”; console.log(myCar[str]); // will show Maruti console.log(myCar.str); // will show undefined
  • 18. Special Point ! Reading from a property that does not exist will result in an undefined. For example: console.log(myCar.price); // undefined
  • 19. Changing Property Value To change the value of a property, we simply use the assignment operator Example: myCar[“name”]=“Baleno”; myCar[“year”]=2018; myCar[“name”]=“Ciaz”; console.log(myCar.name);// Ciaz console.log(myCar.year);// 2018
  • 20. Deleting A Property To delete a property from an object, we use the delete operator: Example: myCar[“name”]=“Baleno”; myCar[“year”]=2018; delete myCar.year; console.log(myCar.name);// Baleno console.log(myCar.year);// undefined
  • 21. Checking Whether A Property Exists Or Not To check if a property does exists in an object, we use a special operator called the in operator: Example: myCar[“name”]=“Baleno”; myCar[“year”]=2018; delete myCar.year; console.log(‘name’ in myCar);// true console.log(‘year’ in myCar);// false
  • 22. Iterating Over Properties To iterate over all properties of an object without knowing property names, we use the for...in loop: Syntax: for (key in object) { // executes the body for each key among object properties } Example: myCar[“name”]=“Baleno”; myCar[“year”]=2018; for (let key in myCar)
  • 23. Adding Methods Methods can also be added to the object A method is nothing but a function that will access/manipulate object’s properties. Methods can be part of object during creation or can be added later like properties
  • 26. Nested Objects We can assign another object as a property of an object. Syntax: <obj_name>.<obj_name>=new Object(); <obj_name>.<obj_name>.<property_name>=<value>; Example: myCar.stereo=new Object(); myCar.stereo.name=“Sony";
  • 27. PROGRAM Write a JavaScript code to access all the properties of myCar objects and it’s nested object using for in loop
  • 28. Creating Object Using Object Literal The object literal is a simple way of creating an object and it is done using { } brackets. We can include key-value pair in { }, where key would be property or method name and value will be value of property or definition of the method. We use comma (,) to separate multiple key-value pairs.
  • 29. Creating Object Using Object Literal Syntax: let <obj_name>={ <prop_name>:<value>, <prop_name>:<value> }; Example: let myCar={ company:”Maruti”, year:2014
  • 30. Adding Method Using Object Literal Syntax: let <obj_name>={ <func_name>:function(){ // body } }; Example: let myCar={start:function(){ console.log(“starting the car”); } };
  • 31. PROGRAM Given the following object , write a for in loop to calculate and print total marks let student = { name: "Sumit“,age: 15, phy: 45,chem: 5 0, maths: 65 };