SlideShare a Scribd company logo
1 of 62
Download to read offline
Object Oriented
JavaScript
Bangalore
2 November 2016
Thennarasan Shanmugam
2
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Agenda
Object Notations
Object Properties
 Data Properties
 Accessor Properties
 Defining Multiple Properties
 Reading Property Attributes
Object Creation
 Factory Pattern
 Constructor Pattern
Prototypes
 Prototype Pattern
 How Prototypes Work
 Combination Constructor Prototype Pattern
3
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Notations
4
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Concept of “Class” not available in JavaScript (up to ES5)
Objects are little different in JavaScript than Class based
Object Oriented Languages such as C#, Java and etc.
defn of Object:
 unordered collection of properties each of which contains a primitive value, object or
function
 it is like a grouping of name-value pairs where the value may be a data or a function
 each property or method is identified by a name that is mapped to a value like a
HashTable
Object Notations
5
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Types of Object Notation
 Object Constructor
• old way of defining objects
 Object Literal
• preferred pattern and more concise way of defining objects
Object Notations (Contd.)
6
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Constructor
 Output
Object Notations (Contd.)
7
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Notations (Contd.)
Object Literal
 Output
8
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Properties
9
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Properties
characteristics of properties can be controlled through the use of
internal-only attributes
internal-only attributes are not directly accessible in JavaScript
To indicate that an attribute is internal, the attribute name is
surrounded by a pair of []
 Ex: [[Writable]]
10
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Properties (Contd.)
Data Properties
 [[Configurable]]
• indicates if the property may be redefined by removing the property via delete, changing
the property’s attributes, or changing the property into an accessor property
 [[Enumerable]]
• indicates if the property will be returned in a for-in loop
 [[Writable]]
• indicates if the property’s value can be changed
 [[Value]]
• contains the actual data value for the property
11
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Properties (Contd.)
Data Properties
 Object.defineProperty(object, “property_name”, descriptorObject);
• By default, data properties except Value are set to false
Output
12
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Properties (Contd.)
Accessor Properties
 [[Configurable]]
• indicates if the property may be redefined by removing the property via delete, changing
the property’s attributes, or changing the property into a data property
 [[Enumerable]]
• indicates if the property will be returned in a for-in loop
 [[Get]]
• function to call when the property is read from, default value is undefined
 [[Set]]
• function to call when the property is written from, default value is undefined
13
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Properties (Contd.)
Accessor Properties
Output
14
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Properties (Contd.)
Defining Multiple Properties
 Object.defineProperties(object, descriptorObject);
Output
15
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Properties (Contd.)
Reading Property Attributes
 Object.getOwnPropertyDescriptor(object, property_name); Output
16
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation
17
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation
Factory Pattern
 well-known design pattern used in software engineering to abstract away the
process of creating objects
 functions are created to encapsulate the creation of objects with specific
interfaces
 solves the problem of creating multiple similar objects
Disadvantage
 didn’t address the issue of object identification
18
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Factory Pattern
Output
19
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Constructor Pattern
 Creates a New Object
 Assign the “this” value of the constructor to the new object (so this sets the
context to the new object)
 Execute the code inside the constructor (adds properties to the new object)
 Returns the new object
Advantages
 No Object being created explicitly
 Properties and Methods are assigned directly onto the “this” object
 No return statement
20
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Constructor Pattern
Output
21
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Constructor as Functions
Output
22
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Problem with Constructors
 downside of constructor’s is that methods are created once for each instance
 hence, functions of same name on different instances are not equivalent
 it doesn’t make sense to have two instances of Function that do the same thing
23
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Problem with Constructors
Output
24
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Problem with Constructors – Solution!
 to resolve the duplicate functions, define the function outside the constructor
 now eat property contains just a pointer to the global eat() function
 hence, all instances of Fruit end up sharing the same eat() function
25
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Problem with Constructors – Solution!
Output
26
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Prototype Pattern
 even though constructor pattern resolves the duplicate function referencing
issue, it creates a clutter in the global scope
 if an object needed multiple methods, that would mean multiple global
functions, all of a sudden custom reference type is no longer nicely grouped in
the code.
 these problems are addressed using the prototype pattern
 each function is created with a prototype property which is an object containing
properties and methods that should be available to instances of a particular
reference type
27
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Object Creation (Contd.)
Prototype Pattern
 benefit of using the prototype is all of its properties and methods are shared among object
instances
 instead of assigning object information in the constructor, they can be assigned directly to
the prototype as below:
28
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
29
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
How Prototypes Work
Book
prototype
.
Book Prototype
constructor
.
name “Harry Potter….”
author “J.K.Rowling”
price 300
SayReview (function)
book1
[[Prototype]]
.
book2
[[Prototype]]
.
30
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Object.prototype
isPrototypeOf()
Object.getPrototypeOf()
Object.__proto__
31
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Object.prototype
isPrototypeOf()
32
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Object.getPrototypeOf()
33
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Object.__proto__
34
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Shadowing prototype Property
35
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
Shadowing prototype Property with Instance Property
Book
prototype
.
Book Prototype
constructor
.
name “Harry Potter….”
author “J.K.Rowling”
price 300
SayReview (function)
book1
[[Prototype]]
.
name “Fantastic Beasts…”
book2
[[Prototype]]
.
36
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Shadowing prototype Property
37
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
hasOwnProperty() & in Operator
 hasOwnProperty() determines if a property exists on the instance or on the prototype
 “in” Operator when used on its own, returns “true” when a property of the given name is
accessible by the object, which is to say that the property may exist on instance or on the
prototype
38
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
hasOwnProperty() & in Operator
Book
prototype
.
Book Prototype
constructor
.
name “Harry Potter….”
author “J.K.Rowling”
price 300
SayReview (function)
book1
[[Prototype]]
.
book2
[[Prototype]]
.
39
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
hasOwnProperty() & in Operator
40
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
hasOwnProperty() & in Operator
Book
prototype
.
Book Prototype
constructor
.
name “Harry Potter….”
author “J.K.Rowling”
price 300
SayReview (function)
book1
[[Prototype]]
.
name “Quidditch through…”
book2
[[Prototype]]
.
41
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
hasPrototypeProperty()
 To determine if the property of an object exists on the prototype, combine the in-
built hasOwnProperty() & “in” Operator as below
 “in” operator will return “true” as long as the property is accessible by the object
 hasOwnProperty() returns “true” only if the property exists in the instance
42
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
hasPrototypeProperty()
43
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Looping in for-in loop – “in” Operator
 all properties that are accessible by the object in instance and prototype will be
enumerated in for-in loop (including the properties for which [[Enumerable]] set
to “false”
44
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Object.keys(object)
45
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Object.getOwnPropertyNames(object)
46
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Alternate Prototype Syntax
47
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Alternate Prototype Syntax – Solution for Constructor Problem!
48
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Dynamic Nature of Prototypes
49
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
Dynamic Nature of Prototypes – Before Prototype Assignment
God
prototype
.
God Prototype
constructor
.
Type “Human”
Max_age 200
Planet “Earth”
human
[[Prototype]]
.
50
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Dynamic Nature of Prototypes – Prototype Overwrite
51
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
Dynamic Nature of Prototypes – After Prototype Assignment
God
prototype
. New God Prototype
constructor
.
Type “Human”
Max_age 200
Planet “Earth”
getBlessings (function)
human
[[Prototype]]
.
God Prototype
constructor
.
52
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Native Object Prototypes
53
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Problem with Prototypes
54
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
Problem with Prototypes
Breakfast
prototype
.
Breakfast Prototype
constructor
.
name “idly”
price 10
side_dishes [“coconut_chutney”, “sambar”]
taste (function)
idly_A2B
[[Prototype]]
.
idly_MTR
[[Prototype]]
.
55
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
Problem with Prototypes – After the new side_dishes push to idly_A2B
Breakfast
prototype
.
Breakfast Prototype
constructor
.
name “idly”
price 10
side_dishes [“coconut_chutney”, “sambar”,
“tomato_chutney”]
taste (function)
idly_A2B
[[Prototype]]
.
idly_MTR
[[Prototype]]
.
56
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Problem with Prototypes – Solution
 Combination Constructor Prototype Pattern
57
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
Problem with Prototypes – Solution
Breakfast
prototype
.
name name
price price
side_dishes [“coconut_chutney”,
“sambar”]
Breakfast Prototype
constructor
.
taste (function)
58
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript (Contd.)
Problem with Prototypes – Solution
59
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
Problem with Prototypes – Solution
Breakfast
prototype
.
name name
price price
side_dishes [“coconut_chutney”,
“sambar”]
Breakfast Prototype
constructor
.
taste (function)
breakfast_A2B
prototype
.
name “idly”
price 10
side_dishes [“coconut_chutney”,
“sambar”]
breakfast_MTR
prototype
.
name “dosa”
price 20
side_dishes [“coconut_chutney”,
“sambar”]
60
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Prototypes in JavaScript
Problem with Prototypes – After the new side_dishes push to breakfast_A2B
Breakfast
prototype
.
name name
price price
side_dishes [“coconut_chutney”,
“sambar”]
Breakfast Prototype
constructor
.
taste (function)
breakfast_A2B
prototype
.
name “idly”
price 10
side_dishes [“coconut_chutney”, “sambar”,
“tomato_chutney”]
breakfast_MTR
prototype
.
name “dosa”
price 20
side_dishes [“coconut_chutney”,
“sambar”]
61
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Summary
Object Notations
Object Properties
 Data Properties
 Accessor Properties
 Defining Multiple Properties
 Reading Property Attributes
Object Creation
 Factory Pattern
 Constructor Pattern
Prototypes
 Prototype Pattern
 How Prototypes Work
 Combination Constructor Prototype Pattern
62
Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016
Thank you…!

More Related Content

Viewers also liked

Hengelo - Piet Muyselaarstraat 34
Hengelo - Piet Muyselaarstraat 34Hengelo - Piet Muyselaarstraat 34
Hengelo - Piet Muyselaarstraat 34
Woningadviseurs
 

Viewers also liked (13)

Twitter no salvara a los editores
Twitter no salvara a los editoresTwitter no salvara a los editores
Twitter no salvara a los editores
 
Hengelo - Piet Muyselaarstraat 34
Hengelo - Piet Muyselaarstraat 34Hengelo - Piet Muyselaarstraat 34
Hengelo - Piet Muyselaarstraat 34
 
Pano ngoai tinh 6.11.2014
Pano ngoai tinh 6.11.2014Pano ngoai tinh 6.11.2014
Pano ngoai tinh 6.11.2014
 
25 선택 정렬
25 선택 정렬25 선택 정렬
25 선택 정렬
 
Prepositions in
Prepositions inPrepositions in
Prepositions in
 
Microsegmentation
MicrosegmentationMicrosegmentation
Microsegmentation
 
Financial Statements
Financial StatementsFinancial Statements
Financial Statements
 
Product life cycle
Product life cycleProduct life cycle
Product life cycle
 
Six Steps To Create A Social Media Marketing Plan At Little Cost
Six Steps To Create A Social Media Marketing Plan At Little CostSix Steps To Create A Social Media Marketing Plan At Little Cost
Six Steps To Create A Social Media Marketing Plan At Little Cost
 
CAMBRIDGE AS HISTORY: MUSSOLINI AND ABYSSINIA
CAMBRIDGE AS HISTORY: MUSSOLINI AND ABYSSINIACAMBRIDGE AS HISTORY: MUSSOLINI AND ABYSSINIA
CAMBRIDGE AS HISTORY: MUSSOLINI AND ABYSSINIA
 
Marketingautomatizálás a gyakorlatban
Marketingautomatizálás a gyakorlatbanMarketingautomatizálás a gyakorlatban
Marketingautomatizálás a gyakorlatban
 
Analýza rizik pro zavedení NERO
Analýza rizik pro zavedení NEROAnalýza rizik pro zavedení NERO
Analýza rizik pro zavedení NERO
 
حقبة من التاريخ
حقبة من التاريخحقبة من التاريخ
حقبة من التاريخ
 

Similar to Object Oriented Javascript

9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
Terry Yoast
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
Sopheak Sem
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
Chamnap Chhorn
 

Similar to Object Oriented Javascript (20)

JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
 
Java scriptforjavadev part2a
Java scriptforjavadev part2aJava scriptforjavadev part2a
Java scriptforjavadev part2a
 
Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based Programming
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
HSC INFORMATION TECHNOLOGY CHAPTER 3 ADVANCED JAVASCRIPT
HSC INFORMATION TECHNOLOGY CHAPTER 3 ADVANCED JAVASCRIPTHSC INFORMATION TECHNOLOGY CHAPTER 3 ADVANCED JAVASCRIPT
HSC INFORMATION TECHNOLOGY CHAPTER 3 ADVANCED JAVASCRIPT
 
Chapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesChapter 11 Working with Events and Styles
Chapter 11 Working with Events and Styles
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
Streamlining JSON mapping
Streamlining JSON mappingStreamlining JSON mapping
Streamlining JSON mapping
 
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
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Metaprogramming in JavaScript
Metaprogramming in JavaScriptMetaprogramming in JavaScript
Metaprogramming in JavaScript
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScript
 
Understanding-Objects-in-Javascript.pptx
Understanding-Objects-in-Javascript.pptxUnderstanding-Objects-in-Javascript.pptx
Understanding-Objects-in-Javascript.pptx
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 

Recently uploaded

6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
imonikaupta
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 

Recently uploaded (20)

6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
Russian Call Girls in %(+971524965298  )#  Call Girls in DubaiRussian Call Girls in %(+971524965298  )#  Call Girls in Dubai
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
 
Hot Call Girls |Delhi |Hauz Khas ☎ 9711199171 Book Your One night Stand
Hot Call Girls |Delhi |Hauz Khas ☎ 9711199171 Book Your One night StandHot Call Girls |Delhi |Hauz Khas ☎ 9711199171 Book Your One night Stand
Hot Call Girls |Delhi |Hauz Khas ☎ 9711199171 Book Your One night Stand
 

Object Oriented Javascript

  • 2. 2 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Agenda Object Notations Object Properties  Data Properties  Accessor Properties  Defining Multiple Properties  Reading Property Attributes Object Creation  Factory Pattern  Constructor Pattern Prototypes  Prototype Pattern  How Prototypes Work  Combination Constructor Prototype Pattern
  • 3. 3 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Notations
  • 4. 4 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Concept of “Class” not available in JavaScript (up to ES5) Objects are little different in JavaScript than Class based Object Oriented Languages such as C#, Java and etc. defn of Object:  unordered collection of properties each of which contains a primitive value, object or function  it is like a grouping of name-value pairs where the value may be a data or a function  each property or method is identified by a name that is mapped to a value like a HashTable Object Notations
  • 5. 5 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Types of Object Notation  Object Constructor • old way of defining objects  Object Literal • preferred pattern and more concise way of defining objects Object Notations (Contd.)
  • 6. 6 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Constructor  Output Object Notations (Contd.)
  • 7. 7 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Notations (Contd.) Object Literal  Output
  • 8. 8 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Properties
  • 9. 9 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Properties characteristics of properties can be controlled through the use of internal-only attributes internal-only attributes are not directly accessible in JavaScript To indicate that an attribute is internal, the attribute name is surrounded by a pair of []  Ex: [[Writable]]
  • 10. 10 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Properties (Contd.) Data Properties  [[Configurable]] • indicates if the property may be redefined by removing the property via delete, changing the property’s attributes, or changing the property into an accessor property  [[Enumerable]] • indicates if the property will be returned in a for-in loop  [[Writable]] • indicates if the property’s value can be changed  [[Value]] • contains the actual data value for the property
  • 11. 11 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Properties (Contd.) Data Properties  Object.defineProperty(object, “property_name”, descriptorObject); • By default, data properties except Value are set to false Output
  • 12. 12 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Properties (Contd.) Accessor Properties  [[Configurable]] • indicates if the property may be redefined by removing the property via delete, changing the property’s attributes, or changing the property into a data property  [[Enumerable]] • indicates if the property will be returned in a for-in loop  [[Get]] • function to call when the property is read from, default value is undefined  [[Set]] • function to call when the property is written from, default value is undefined
  • 13. 13 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Properties (Contd.) Accessor Properties Output
  • 14. 14 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Properties (Contd.) Defining Multiple Properties  Object.defineProperties(object, descriptorObject); Output
  • 15. 15 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Properties (Contd.) Reading Property Attributes  Object.getOwnPropertyDescriptor(object, property_name); Output
  • 16. 16 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation
  • 17. 17 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation Factory Pattern  well-known design pattern used in software engineering to abstract away the process of creating objects  functions are created to encapsulate the creation of objects with specific interfaces  solves the problem of creating multiple similar objects Disadvantage  didn’t address the issue of object identification
  • 18. 18 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Factory Pattern Output
  • 19. 19 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Constructor Pattern  Creates a New Object  Assign the “this” value of the constructor to the new object (so this sets the context to the new object)  Execute the code inside the constructor (adds properties to the new object)  Returns the new object Advantages  No Object being created explicitly  Properties and Methods are assigned directly onto the “this” object  No return statement
  • 20. 20 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Constructor Pattern Output
  • 21. 21 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Constructor as Functions Output
  • 22. 22 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Problem with Constructors  downside of constructor’s is that methods are created once for each instance  hence, functions of same name on different instances are not equivalent  it doesn’t make sense to have two instances of Function that do the same thing
  • 23. 23 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Problem with Constructors Output
  • 24. 24 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Problem with Constructors – Solution!  to resolve the duplicate functions, define the function outside the constructor  now eat property contains just a pointer to the global eat() function  hence, all instances of Fruit end up sharing the same eat() function
  • 25. 25 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Problem with Constructors – Solution! Output
  • 26. 26 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Prototype Pattern  even though constructor pattern resolves the duplicate function referencing issue, it creates a clutter in the global scope  if an object needed multiple methods, that would mean multiple global functions, all of a sudden custom reference type is no longer nicely grouped in the code.  these problems are addressed using the prototype pattern  each function is created with a prototype property which is an object containing properties and methods that should be available to instances of a particular reference type
  • 27. 27 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Object Creation (Contd.) Prototype Pattern  benefit of using the prototype is all of its properties and methods are shared among object instances  instead of assigning object information in the constructor, they can be assigned directly to the prototype as below:
  • 28. 28 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript
  • 29. 29 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript How Prototypes Work Book prototype . Book Prototype constructor . name “Harry Potter….” author “J.K.Rowling” price 300 SayReview (function) book1 [[Prototype]] . book2 [[Prototype]] .
  • 30. 30 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Object.prototype isPrototypeOf() Object.getPrototypeOf() Object.__proto__
  • 31. 31 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Object.prototype isPrototypeOf()
  • 32. 32 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Object.getPrototypeOf()
  • 33. 33 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Object.__proto__
  • 34. 34 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Shadowing prototype Property
  • 35. 35 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript Shadowing prototype Property with Instance Property Book prototype . Book Prototype constructor . name “Harry Potter….” author “J.K.Rowling” price 300 SayReview (function) book1 [[Prototype]] . name “Fantastic Beasts…” book2 [[Prototype]] .
  • 36. 36 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Shadowing prototype Property
  • 37. 37 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) hasOwnProperty() & in Operator  hasOwnProperty() determines if a property exists on the instance or on the prototype  “in” Operator when used on its own, returns “true” when a property of the given name is accessible by the object, which is to say that the property may exist on instance or on the prototype
  • 38. 38 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript hasOwnProperty() & in Operator Book prototype . Book Prototype constructor . name “Harry Potter….” author “J.K.Rowling” price 300 SayReview (function) book1 [[Prototype]] . book2 [[Prototype]] .
  • 39. 39 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) hasOwnProperty() & in Operator
  • 40. 40 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript hasOwnProperty() & in Operator Book prototype . Book Prototype constructor . name “Harry Potter….” author “J.K.Rowling” price 300 SayReview (function) book1 [[Prototype]] . name “Quidditch through…” book2 [[Prototype]] .
  • 41. 41 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) hasPrototypeProperty()  To determine if the property of an object exists on the prototype, combine the in- built hasOwnProperty() & “in” Operator as below  “in” operator will return “true” as long as the property is accessible by the object  hasOwnProperty() returns “true” only if the property exists in the instance
  • 42. 42 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) hasPrototypeProperty()
  • 43. 43 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Looping in for-in loop – “in” Operator  all properties that are accessible by the object in instance and prototype will be enumerated in for-in loop (including the properties for which [[Enumerable]] set to “false”
  • 44. 44 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Object.keys(object)
  • 45. 45 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Object.getOwnPropertyNames(object)
  • 46. 46 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Alternate Prototype Syntax
  • 47. 47 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Alternate Prototype Syntax – Solution for Constructor Problem!
  • 48. 48 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Dynamic Nature of Prototypes
  • 49. 49 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript Dynamic Nature of Prototypes – Before Prototype Assignment God prototype . God Prototype constructor . Type “Human” Max_age 200 Planet “Earth” human [[Prototype]] .
  • 50. 50 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Dynamic Nature of Prototypes – Prototype Overwrite
  • 51. 51 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript Dynamic Nature of Prototypes – After Prototype Assignment God prototype . New God Prototype constructor . Type “Human” Max_age 200 Planet “Earth” getBlessings (function) human [[Prototype]] . God Prototype constructor .
  • 52. 52 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Native Object Prototypes
  • 53. 53 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Problem with Prototypes
  • 54. 54 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript Problem with Prototypes Breakfast prototype . Breakfast Prototype constructor . name “idly” price 10 side_dishes [“coconut_chutney”, “sambar”] taste (function) idly_A2B [[Prototype]] . idly_MTR [[Prototype]] .
  • 55. 55 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript Problem with Prototypes – After the new side_dishes push to idly_A2B Breakfast prototype . Breakfast Prototype constructor . name “idly” price 10 side_dishes [“coconut_chutney”, “sambar”, “tomato_chutney”] taste (function) idly_A2B [[Prototype]] . idly_MTR [[Prototype]] .
  • 56. 56 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Problem with Prototypes – Solution  Combination Constructor Prototype Pattern
  • 57. 57 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript Problem with Prototypes – Solution Breakfast prototype . name name price price side_dishes [“coconut_chutney”, “sambar”] Breakfast Prototype constructor . taste (function)
  • 58. 58 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript (Contd.) Problem with Prototypes – Solution
  • 59. 59 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript Problem with Prototypes – Solution Breakfast prototype . name name price price side_dishes [“coconut_chutney”, “sambar”] Breakfast Prototype constructor . taste (function) breakfast_A2B prototype . name “idly” price 10 side_dishes [“coconut_chutney”, “sambar”] breakfast_MTR prototype . name “dosa” price 20 side_dishes [“coconut_chutney”, “sambar”]
  • 60. 60 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Prototypes in JavaScript Problem with Prototypes – After the new side_dishes push to breakfast_A2B Breakfast prototype . name name price price side_dishes [“coconut_chutney”, “sambar”] Breakfast Prototype constructor . taste (function) breakfast_A2B prototype . name “idly” price 10 side_dishes [“coconut_chutney”, “sambar”, “tomato_chutney”] breakfast_MTR prototype . name “dosa” price 20 side_dishes [“coconut_chutney”, “sambar”]
  • 61. 61 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Summary Object Notations Object Properties  Data Properties  Accessor Properties  Defining Multiple Properties  Reading Property Attributes Object Creation  Factory Pattern  Constructor Pattern Prototypes  Prototype Pattern  How Prototypes Work  Combination Constructor Prototype Pattern
  • 62. 62 Object Oriented JavaScript | Thennarasan Shanmugam | 2 November 2016 Thank you…!