SlideShare uma empresa Scribd logo
1 de 62
Baixar para ler 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…!

Mais conteúdo relacionado

Destaque

Twitter no salvara a los editores
Twitter no salvara a los editoresTwitter no salvara a los editores
Twitter no salvara a los editoresMiguel Paz
 
Hengelo - Piet Muyselaarstraat 34
Hengelo - Piet Muyselaarstraat 34Hengelo - Piet Muyselaarstraat 34
Hengelo - Piet Muyselaarstraat 34Woningadviseurs
 
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 CostChad Wiebesick
 
CAMBRIDGE AS HISTORY: MUSSOLINI AND ABYSSINIA
CAMBRIDGE AS HISTORY: MUSSOLINI AND ABYSSINIACAMBRIDGE AS HISTORY: MUSSOLINI AND ABYSSINIA
CAMBRIDGE AS HISTORY: MUSSOLINI AND ABYSSINIAGeorge Dumitrache
 
Marketingautomatizálás a gyakorlatban
Marketingautomatizálás a gyakorlatbanMarketingautomatizálás a gyakorlatban
Marketingautomatizálás a gyakorlatbanKata Györke -Szász
 
حقبة من التاريخ
حقبة من التاريخحقبة من التاريخ
حقبة من التاريخOm Muktar
 

Destaque (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
 
حقبة من التاريخ
حقبة من التاريخحقبة من التاريخ
حقبة من التاريخ
 

Semelhante a Object Oriented Javascript

JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetHDR1001
 
Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingDr. Ahmed Al Zaidy
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
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 JAVASCRIPTAAFREEN SHAIKH
 
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 StylesDr. Ahmed Al Zaidy
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS ImplimentationUsman Mehmood
 
Streamlining JSON mapping
Streamlining JSON mappingStreamlining JSON mapping
Streamlining JSON mappingJonathan Lehr
 
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 JavascriptIban Martinez
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayChamnap Chhorn
 
Metaprogramming in JavaScript
Metaprogramming in JavaScriptMetaprogramming in JavaScript
Metaprogramming in JavaScriptMehdi Valikhani
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptDr. Ahmed Al Zaidy
 
Understanding-Objects-in-Javascript.pptx
Understanding-Objects-in-Javascript.pptxUnderstanding-Objects-in-Javascript.pptx
Understanding-Objects-in-Javascript.pptxMariaTrinidadTumanga
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Jalpesh Vasa
 

Semelhante a 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
 

Último

20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdfMatthew Sinclair
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoilmeghakumariji156
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC
 
75539-Cyber Security Challenges PPT.pptx
75539-Cyber Security Challenges PPT.pptx75539-Cyber Security Challenges PPT.pptx
75539-Cyber Security Challenges PPT.pptxAsmae Rabhi
 
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Roommeghakumariji156
 
PowerDirector Explination Process...pptx
PowerDirector Explination Process...pptxPowerDirector Explination Process...pptx
PowerDirector Explination Process...pptxgalaxypingy
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查ydyuyu
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"growthgrids
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...kajalverma014
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制pxcywzqs
 
Power point inglese - educazione civica di Nuria Iuzzolino
Power point inglese - educazione civica di Nuria IuzzolinoPower point inglese - educazione civica di Nuria Iuzzolino
Power point inglese - educazione civica di Nuria Iuzzolinonuriaiuzzolino1
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsMonica Sydney
 
Microsoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftMicrosoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftAanSulistiyo
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsMonica Sydney
 
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 sweatshirtrahman018755
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdfMatthew Sinclair
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsMonica Sydney
 
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 GraphsEleniIlkou
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样ayvbos
 

Último (20)

20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
75539-Cyber Security Challenges PPT.pptx
75539-Cyber Security Challenges PPT.pptx75539-Cyber Security Challenges PPT.pptx
75539-Cyber Security Challenges PPT.pptx
 
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
PowerDirector Explination Process...pptx
PowerDirector Explination Process...pptxPowerDirector Explination Process...pptx
PowerDirector Explination Process...pptx
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
 
Power point inglese - educazione civica di Nuria Iuzzolino
Power point inglese - educazione civica di Nuria IuzzolinoPower point inglese - educazione civica di Nuria Iuzzolino
Power point inglese - educazione civica di Nuria Iuzzolino
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
 
Microsoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftMicrosoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck Microsoft
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 
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
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
 
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
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
 

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…!