SlideShare a Scribd company logo
1 of 27
JavaScript Objects and
Functions
Dhananjay Kumar [@debug_mode]
Delhi Chapter Lead
Microsoft MVP
Mindcracker MVP
Telerik Evangelist
http://debugmode.net
FB: Dhananjay.25july@gmail.com
JavaScript Objects
• There are three ways you can create JavaScript Objects
JavaScript
Objects
Using Literals
Using New
Operator
Using
Object.Create()
JavaScript Objects
Using Literals
var student
= {
name: "dj",
grade : "z"
};
var studentName = student.name;
alert(studentName);
Object literal is an expression
It creates new object each time it appears
in the code
A single object literal can create many
objects in loop
JavaScript Objects
Using new operator
var student = new Object();
var studentArray = new Array();
new operator creates a new object
new operator initialize created
object also
new operator invokes a function as
give in above code snippet.
Function invoked after new
operator is Constructor
JavaScript Objects
Using Object.Create()
var student = Object.create(
{
name: "dj",
grade: 10
}
);
It is a static function
It always has two parameters
• Prototype
• Properties
JavaScript Objects
• There are three ways you can test Properties of JavaScript Objects
JavaScript Objects
Properties
in operator
hasOwnProperty()
method
propertyIsEnumerable()
method
JavaScript Objects
in operator
var student = Object.create(
{
name: "dj",
grade: 10
}
);
var studentProperty = "name" in
student
alert(studentProperty);
in operator returns true for own property
in operator returns false if there is no
property with given name
in operator returns true for inherited
property
JavaScript Objects
hasOwnProperty() method
var student =
{
name: "dj",
grade: 10
};
var studentProperty =
student.hasOwnProperty("name");
alert(studentProperty);
hasOwnProperty() method returns true
for own property
hasOwnProperty() method returns false if
there is no property with given name
hasOwnProperty() method returns false
for inherited property
JavaScript Objects
propertyIsEnumerable () method
var student =
{
name: "dj",
grade: 10
};
var studentProperty =
student.hasOwnProperty("name");
alert(studentProperty);
propertyIsEnumerable () method returns
true for own property which is
enumerable
propertyIsEnumerable () method returns
false if there is no property with given
name
propertyIsEnumerable () method returns
false for inherited property
JavaScript Objects
• Deleting Properties : using delete operator
Delete operator
returns
true if
successfully
deleted
If there is no
effect on
operation
JavaScript Objects
Deleting properties
var student =
{
name: "dj",
grade: 10
};
var result = delete student.grade;
alert(result);
var result1 = delete student["grade"];
alert(result1);
 It deletes object’s own property
 It does not delete inherited property. For example you cannot
delete property of student prototype or object it is inheriting
from.
Associative Array
Associative Array : Java
Script Array can be worked
with string index
var arrayofStudent = [];
var student1 = [];
student1["rollnumber"] = 19;
student1["name"] = "John";
student1["grade"] = "A";
arrayofStudent.push(student1);
console.log(arrayofStudent[0]["name"]);
Associative Array is that we can not traverse through array
elements in a loop.
JavaScript object as Associative Array
var student =
{
name: "dj",
grade: 10
};
for (i = 0; i < 10; i++) {
student["marks" + i] = 100 * i;
}
alert(student.marks8);
In Square bracket you
provide property name as
string. String is data type so
can be manipulated at run
time.
With dot you provide
property name as identifier.
Since they are not a data type
so cannot be manipulated at
run time.
For-in loop in JavaScript
Functions in JavaScript
var display_message = function
(age) {
alert("you are " + age +
"year old ");
};
display_message(19);
JavaScript
Functions
Named
Function
Anonymous
Function
Functions in JavaScript
 You can store it in a variable
You can pass it as parameter of
other function
You can use it in an expression
Functions in JavaScript
Pass by Value
var greetingmessage = "Hey DJ";
function showmessage(greetingmessage)
{
greetingmessage = "I am changing
value";
}
showmessage(greetingmessage);
alert(greetingmessage);
Pass By Reference
var arrayofname = ["dj"];
function showmessage(arrayofname) {
arrayofname[0] = "dhananjay";
}
showmessage(arrayofname);
alert(arrayofname[0]);
Functions in JavaScript
Nested Functions
mainfunction(7, 6);
function mainfunction(a, b) {
alert(a);
nestedfunction(88);
function nestedfunction(c) {
alert(b);
};
};
 Nested function can access variable of parent function
 Parent function cannot access variable of nested function
Functions in JavaScript
Namespace in JavaScript
Global if defined outside
any function
Local to function and all
nested function , if
defined in function
var MyModule = {
//code for module here
GetData: function () {
var student =
{
name: "dj",
grade: 10
};
var nameisobjectofstudent = "toString" in student;
alert(nameisobjectofstudent);
}
};
Functions in JavaScript
Namespace in JavaScript
Global if defined outside
any function
Local to function and all
nested function , if
defined in function
var MyModule = {
//code for module here
GetData: function () {
var student =
{
name: "dj",
grade: 10
};
var nameisobjectofstudent = "toString" in student;
alert(nameisobjectofstudent);
}
};
Functions in JavaScript
Namespace in JavaScript
Global if defined outside
any function
Local to function and all
nested function , if
defined in function
var studentObject =
{
name: "dj",
marks: 89,
findgrade: function (marks) {
if (marks > 75) {
return "Grade A ";
}
else {
return "Grade B ";
}
}
}
var grade = studentObject.findgrade(99);
alert(grade);
Functions in JavaScript
 It takes a function as argument
 It returns a function as output.
Functions in JavaScript
JavaScript function
Does not do any type checking
on argument values
Does not do any checking on
number of arguments passed
JavaScript function can be called
1. With exact number of
arguments
2. With more number of
arguments than specified
3. With less number of
arguments than specified
Functions in JavaScript
Less Number of Arguments
Functions in JavaScript
Less Number of Arguments
Functions in JavaScript
Less Number of Arguments
Handling undefined
Functions in JavaScript
More Number of Arguments

More Related Content

What's hot

JavaScript Basics - GameCraft Training
JavaScript Basics - GameCraft TrainingJavaScript Basics - GameCraft Training
JavaScript Basics - GameCraft TrainingRadoslav Georgiev
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetHDR1001
 
Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3BeeNear
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptNascenia IT
 
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Andrea Zaza
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)raja kvk
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.Brian Hsu
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayChamnap Chhorn
 
#pugMi - DDD - Value objects
#pugMi - DDD - Value objects#pugMi - DDD - Value objects
#pugMi - DDD - Value objectsSimone Gentili
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in PythonJoshua Forman
 
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...GeeksLab Odessa
 
LinkedIn TBC JavaScript 100: Intro
LinkedIn TBC JavaScript 100: IntroLinkedIn TBC JavaScript 100: Intro
LinkedIn TBC JavaScript 100: IntroAdam Crabtree
 

What's hot (20)

JavsScript OOP
JavsScript OOPJavsScript OOP
JavsScript OOP
 
JavaScript Basics - GameCraft Training
JavaScript Basics - GameCraft TrainingJavaScript Basics - GameCraft Training
JavaScript Basics - GameCraft Training
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
 
Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Week3
Week3Week3
Week3
 
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.
 
Javascript analysis
Javascript analysisJavascript analysis
Javascript analysis
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
#pugMi - DDD - Value objects
#pugMi - DDD - Value objects#pugMi - DDD - Value objects
#pugMi - DDD - Value objects
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
 
Prototype
PrototypePrototype
Prototype
 
Java script
Java scriptJava script
Java script
 
Core concepts-javascript
Core concepts-javascriptCore concepts-javascript
Core concepts-javascript
 
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
 
LinkedIn TBC JavaScript 100: Intro
LinkedIn TBC JavaScript 100: IntroLinkedIn TBC JavaScript 100: Intro
LinkedIn TBC JavaScript 100: Intro
 

Similar to Functions and Objects in JavaScript

Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascriptDoeun KOCH
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript BasicsMindfire Solutions
 
Java script object model
Java script object modelJava script object model
Java script object modelJames Hsieh
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashBret Little
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxMegha V
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Sopheak Sem
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptJulie Iskander
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & AnswersRatnala Charan kumar
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxDrYogeshDeshmukh1
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...Doug Jones
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptDonald Sipe
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript MisunderstoodBhavya Siddappa
 

Similar to Functions and Objects in JavaScript (20)

Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Java script object model
Java script object modelJava script object model
Java script object model
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and Lodash
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
 
Enterprise js pratices
Enterprise js praticesEnterprise js pratices
Enterprise js pratices
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Lecture 6: Client Side Programming 2
Lecture 6: Client Side Programming 2Lecture 6: Client Side Programming 2
Lecture 6: Client Side Programming 2
 
Only oop
Only oopOnly oop
Only oop
 
Oop
OopOop
Oop
 
Json generation
Json generationJson generation
Json generation
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript Misunderstood
 

More from Dhananjay Kumar

Slides of webinar Kendo UI and Knockout.js
Slides of webinar Kendo UI and Knockout.jsSlides of webinar Kendo UI and Knockout.js
Slides of webinar Kendo UI and Knockout.jsDhananjay Kumar
 
Presenter deck icenium hol
Presenter deck   icenium holPresenter deck   icenium hol
Presenter deck icenium holDhananjay Kumar
 
Windows azure mobile service
Windows azure mobile serviceWindows azure mobile service
Windows azure mobile serviceDhananjay Kumar
 
Test studiowebinaraugcodedstep
Test studiowebinaraugcodedstepTest studiowebinaraugcodedstep
Test studiowebinaraugcodedstepDhananjay Kumar
 
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile Dhananjay Kumar
 
Cloud Based Enterprise Apps using Everlive
Cloud Based Enterprise Apps using EverliveCloud Based Enterprise Apps using Everlive
Cloud Based Enterprise Apps using EverliveDhananjay Kumar
 
A Look into Automated Web UI Test
A Look into Automated Web UI TestA Look into Automated Web UI Test
A Look into Automated Web UI TestDhananjay Kumar
 
Windows phone 8 app using Kendo UI
Windows phone 8 app using Kendo UI Windows phone 8 app using Kendo UI
Windows phone 8 app using Kendo UI Dhananjay Kumar
 
Windows aazuremobileservices
Windows aazuremobileservicesWindows aazuremobileservices
Windows aazuremobileservicesDhananjay Kumar
 
Rad controlforwindows25thapril
Rad controlforwindows25thaprilRad controlforwindows25thapril
Rad controlforwindows25thaprilDhananjay Kumar
 
Windows storemindcrcaker23rdmarch
Windows storemindcrcaker23rdmarchWindows storemindcrcaker23rdmarch
Windows storemindcrcaker23rdmarchDhananjay Kumar
 

More from Dhananjay Kumar (20)

Slides of webinar Kendo UI and Knockout.js
Slides of webinar Kendo UI and Knockout.jsSlides of webinar Kendo UI and Knockout.js
Slides of webinar Kendo UI and Knockout.js
 
Nodejsvs
NodejsvsNodejsvs
Nodejsvs
 
Node.js
Node.jsNode.js
Node.js
 
No SQL with Kendo UI
No SQL with Kendo UI No SQL with Kendo UI
No SQL with Kendo UI
 
Patterns in JavaScript
Patterns in JavaScriptPatterns in JavaScript
Patterns in JavaScript
 
Presenter deck icenium hol
Presenter deck   icenium holPresenter deck   icenium hol
Presenter deck icenium hol
 
Bringbestoinyou
BringbestoinyouBringbestoinyou
Bringbestoinyou
 
Java script
Java scriptJava script
Java script
 
Windows azure mobile service
Windows azure mobile serviceWindows azure mobile service
Windows azure mobile service
 
Test studiowebinaraugcodedstep
Test studiowebinaraugcodedstepTest studiowebinaraugcodedstep
Test studiowebinaraugcodedstep
 
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
 
Cloud Based Enterprise Apps using Everlive
Cloud Based Enterprise Apps using EverliveCloud Based Enterprise Apps using Everlive
Cloud Based Enterprise Apps using Everlive
 
A Look into Automated Web UI Test
A Look into Automated Web UI TestA Look into Automated Web UI Test
A Look into Automated Web UI Test
 
Windows phone 8 app using Kendo UI
Windows phone 8 app using Kendo UI Windows phone 8 app using Kendo UI
Windows phone 8 app using Kendo UI
 
Cross platformmobileapp
Cross platformmobileappCross platformmobileapp
Cross platformmobileapp
 
Windows aazuremobileservices
Windows aazuremobileservicesWindows aazuremobileservices
Windows aazuremobileservices
 
Rad controlforwindows25thapril
Rad controlforwindows25thaprilRad controlforwindows25thapril
Rad controlforwindows25thapril
 
Data asservice
Data asserviceData asservice
Data asservice
 
WCF for begineers
WCF  for begineersWCF  for begineers
WCF for begineers
 
Windows storemindcrcaker23rdmarch
Windows storemindcrcaker23rdmarchWindows storemindcrcaker23rdmarch
Windows storemindcrcaker23rdmarch
 

Recently uploaded

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

Functions and Objects in JavaScript

  • 1. JavaScript Objects and Functions Dhananjay Kumar [@debug_mode] Delhi Chapter Lead Microsoft MVP Mindcracker MVP Telerik Evangelist http://debugmode.net FB: Dhananjay.25july@gmail.com
  • 2. JavaScript Objects • There are three ways you can create JavaScript Objects JavaScript Objects Using Literals Using New Operator Using Object.Create()
  • 3. JavaScript Objects Using Literals var student = { name: "dj", grade : "z" }; var studentName = student.name; alert(studentName); Object literal is an expression It creates new object each time it appears in the code A single object literal can create many objects in loop
  • 4. JavaScript Objects Using new operator var student = new Object(); var studentArray = new Array(); new operator creates a new object new operator initialize created object also new operator invokes a function as give in above code snippet. Function invoked after new operator is Constructor
  • 5. JavaScript Objects Using Object.Create() var student = Object.create( { name: "dj", grade: 10 } ); It is a static function It always has two parameters • Prototype • Properties
  • 6. JavaScript Objects • There are three ways you can test Properties of JavaScript Objects JavaScript Objects Properties in operator hasOwnProperty() method propertyIsEnumerable() method
  • 7. JavaScript Objects in operator var student = Object.create( { name: "dj", grade: 10 } ); var studentProperty = "name" in student alert(studentProperty); in operator returns true for own property in operator returns false if there is no property with given name in operator returns true for inherited property
  • 8. JavaScript Objects hasOwnProperty() method var student = { name: "dj", grade: 10 }; var studentProperty = student.hasOwnProperty("name"); alert(studentProperty); hasOwnProperty() method returns true for own property hasOwnProperty() method returns false if there is no property with given name hasOwnProperty() method returns false for inherited property
  • 9. JavaScript Objects propertyIsEnumerable () method var student = { name: "dj", grade: 10 }; var studentProperty = student.hasOwnProperty("name"); alert(studentProperty); propertyIsEnumerable () method returns true for own property which is enumerable propertyIsEnumerable () method returns false if there is no property with given name propertyIsEnumerable () method returns false for inherited property
  • 10. JavaScript Objects • Deleting Properties : using delete operator Delete operator returns true if successfully deleted If there is no effect on operation
  • 11. JavaScript Objects Deleting properties var student = { name: "dj", grade: 10 }; var result = delete student.grade; alert(result); var result1 = delete student["grade"]; alert(result1);  It deletes object’s own property  It does not delete inherited property. For example you cannot delete property of student prototype or object it is inheriting from.
  • 12. Associative Array Associative Array : Java Script Array can be worked with string index var arrayofStudent = []; var student1 = []; student1["rollnumber"] = 19; student1["name"] = "John"; student1["grade"] = "A"; arrayofStudent.push(student1); console.log(arrayofStudent[0]["name"]); Associative Array is that we can not traverse through array elements in a loop.
  • 13. JavaScript object as Associative Array var student = { name: "dj", grade: 10 }; for (i = 0; i < 10; i++) { student["marks" + i] = 100 * i; } alert(student.marks8); In Square bracket you provide property name as string. String is data type so can be manipulated at run time. With dot you provide property name as identifier. Since they are not a data type so cannot be manipulated at run time.
  • 14. For-in loop in JavaScript
  • 15. Functions in JavaScript var display_message = function (age) { alert("you are " + age + "year old "); }; display_message(19); JavaScript Functions Named Function Anonymous Function
  • 16. Functions in JavaScript  You can store it in a variable You can pass it as parameter of other function You can use it in an expression
  • 17. Functions in JavaScript Pass by Value var greetingmessage = "Hey DJ"; function showmessage(greetingmessage) { greetingmessage = "I am changing value"; } showmessage(greetingmessage); alert(greetingmessage); Pass By Reference var arrayofname = ["dj"]; function showmessage(arrayofname) { arrayofname[0] = "dhananjay"; } showmessage(arrayofname); alert(arrayofname[0]);
  • 18. Functions in JavaScript Nested Functions mainfunction(7, 6); function mainfunction(a, b) { alert(a); nestedfunction(88); function nestedfunction(c) { alert(b); }; };  Nested function can access variable of parent function  Parent function cannot access variable of nested function
  • 19. Functions in JavaScript Namespace in JavaScript Global if defined outside any function Local to function and all nested function , if defined in function var MyModule = { //code for module here GetData: function () { var student = { name: "dj", grade: 10 }; var nameisobjectofstudent = "toString" in student; alert(nameisobjectofstudent); } };
  • 20. Functions in JavaScript Namespace in JavaScript Global if defined outside any function Local to function and all nested function , if defined in function var MyModule = { //code for module here GetData: function () { var student = { name: "dj", grade: 10 }; var nameisobjectofstudent = "toString" in student; alert(nameisobjectofstudent); } };
  • 21. Functions in JavaScript Namespace in JavaScript Global if defined outside any function Local to function and all nested function , if defined in function var studentObject = { name: "dj", marks: 89, findgrade: function (marks) { if (marks > 75) { return "Grade A "; } else { return "Grade B "; } } } var grade = studentObject.findgrade(99); alert(grade);
  • 22. Functions in JavaScript  It takes a function as argument  It returns a function as output.
  • 23. Functions in JavaScript JavaScript function Does not do any type checking on argument values Does not do any checking on number of arguments passed JavaScript function can be called 1. With exact number of arguments 2. With more number of arguments than specified 3. With less number of arguments than specified
  • 24. Functions in JavaScript Less Number of Arguments
  • 25. Functions in JavaScript Less Number of Arguments
  • 26. Functions in JavaScript Less Number of Arguments Handling undefined
  • 27. Functions in JavaScript More Number of Arguments