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
};

More Related Content

PPTX
Prototype Object.pptx
PDF
Intro to iOS Development • Made by Many
PDF
Constructors In Java – Unveiling Object Creation
PPTX
COMP111-Week-1_138439.pptx
PDF
22519 - Client-Side Scripting Language (CSS) chapter 1 notes .pdf
PPTX
Javascriptinobject orientedway-090512225827-phpapp02
PPT
Class methods
PPT
creating objects and Class methods
Prototype Object.pptx
Intro to iOS Development • Made by Many
Constructors In Java – Unveiling Object Creation
COMP111-Week-1_138439.pptx
22519 - Client-Side Scripting Language (CSS) chapter 1 notes .pdf
Javascriptinobject orientedway-090512225827-phpapp02
Class methods
creating objects and Class methods

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

PDF
Object Oriented PHP - PART-1
PPTX
IWT presentation121232112122222225556+556.pptx
PDF
Session 3 - Object oriented programming with Objective-C (part 1)
ODP
(An Extended) Beginners Guide to Object Orientation in PHP
PPTX
Android App code starter
PDF
Swift Tutorial Part 2. The complete guide for Swift programming language
PDF
QTP Descriptive programming Tutorial
PPT
Objective c
PPTX
object oriented programing lecture 1
PDF
Design patterns in javascript
DOCX
OOP and C++Classes
PPTX
Lecture 4- Javascript Function presentation
PPT
Lecture 9
PPT
ActionScript 3.0 Fundamentals
PPT
Md02 - Getting Started part-2
PPTX
Lecture 4. mte 407
PPT
Oop java
PDF
Intro to Objective C
DOCX
New microsoft office word document (2)
PPT
eXo SEA - JavaScript Introduction Training
Object Oriented PHP - PART-1
IWT presentation121232112122222225556+556.pptx
Session 3 - Object oriented programming with Objective-C (part 1)
(An Extended) Beginners Guide to Object Orientation in PHP
Android App code starter
Swift Tutorial Part 2. The complete guide for Swift programming language
QTP Descriptive programming Tutorial
Objective c
object oriented programing lecture 1
Design patterns in javascript
OOP and C++Classes
Lecture 4- Javascript Function presentation
Lecture 9
ActionScript 3.0 Fundamentals
Md02 - Getting Started part-2
Lecture 4. mte 407
Oop java
Intro to Objective C
New microsoft office word document (2)
eXo SEA - JavaScript Introduction Training
Ad

Recently uploaded (20)

PDF
Complications of Minimal Access-Surgery.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
Empowerment Technology for Senior High School Guide
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
HVAC Specification 2024 according to central public works department
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
English Textual Question & Ans (12th Class).pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
semiconductor packaging in vlsi design fab
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
Education and Perspectives of Education.pptx
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
advance database management system book.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Complications of Minimal Access-Surgery.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
Empowerment Technology for Senior High School Guide
Race Reva University – Shaping Future Leaders in Artificial Intelligence
Uderstanding digital marketing and marketing stratergie for engaging the digi...
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Core Concepts of Personalized Learning and Virtual Learning Environments
HVAC Specification 2024 according to central public works department
Share_Module_2_Power_conflict_and_negotiation.pptx
English Textual Question & Ans (12th Class).pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
semiconductor packaging in vlsi design fab
B.Sc. DS Unit 2 Software Engineering.pptx
Education and Perspectives of Education.pptx
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
advance database management system book.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
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 };