SlideShare a Scribd company logo
1 of 17
Patterns in Javascript
Prototype & Module Patterns

Presentation By :
Ashutosh Mahto
Mindfire Solutions
Patterns In Javascript

Agenda to Discuss
1. Why to concern about design patterns in Javascript?
2. Before we begin what needs to be known?
3. Prototype Pattern – Structure, Advantages & Drawbacks
4. Module Pattern – Structure, Advantages & Drawbacks
5. Some Enhanced Patterns –
Revealing Module Pattern
Revealing Prototype Pattern
Patterns In Javascript

Why a Design Pattern
1. Scattered variables and function
2. Conflicts may arise between variables and methods
3. Difficult to manage when code becomes larger
4. Often result into repeated functions for similar purpose
Patterns In Javascript

Why a Design Pattern
”A pattern is a reusable solution that can be applied to
commonly occurring problems in software design”
Benefits of choosing a design pattern 1. Patterns are proven solutions
2. Patterns can be easily reused
3. Patterns can be expressive
Patterns In Javascript

Before we begin
Namespaces
- Reduces number of globals and chance of scattered globals
- Avoids naming conflicts
Ex. var MFLib = MFLib || {};

Closures
- Local variables for a function are kept alive after the function has
returned
- Used widely to differentiate public and private members in javascript

Public and Private members in Javascript
- Javascript doesn't have special syntax for public and private
- Can be implemented using closures
Patterns In Javascript

Before we begin
Prototypes
- Prototype is the base of Object Oriented Programming in
javascript
- Every function contains a prototype object that can be
chained through its constructor.
var Person= function(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var student = new Person("Satish");
Patterns In Javascript

Prototype Pattern : Structure
Prototype For Product
MFLib.Product = function (name) {
this.Id = '';
}
MFLib.Product.prototype.setPrice = function (price) {}
MFLib.Product.prototype.setDescription = function
(description) {}
Patterns In Javascript

Prototype Pattern : Advantages & Drawbacks
Advantages
1. Modularizes and isolates the code
2. Separates related variables and methods from global
context
3. Easy to be extended through prototype

Drawbacks
1. Restricts access to private members
Patterns In Javascript

Module Pattern : Structure
Shopping Cart Module
MFLib.ShoppingCart = (function () {
/* Private Variables */
var _basket = [];
/* Private Method */
function getShoppingCartTotal() {}
return {
CreateBasketItem: function () {},
AddItem: function () {},
RemoveItem: function () {},
GetTotal: getShoppingCartTotal
};
})();
Patterns In Javascript

Module Pattern : Structure
Shopping Cart Module
MFLib.ShoppingCart = (function () {
/* Private Variables */
var _basket = [];
/* Private Method */
function getShoppingCartTotal() {}
return {
CreateBasketItem: function () {},
AddItem: function () {},
RemoveItem: function () {},
GetTotal: getShoppingCartTotal
};
})();
Patterns In Javascript

Module Pattern : Important Points
Global Import
By passing globals as parameters to our anonymous function, we import them
into our code
MFLib.ShoppingCart = (function ($,window,document,undefined) {
})(jQuery,window,document,undefined);

Augmentation
MFLib.ShoppingCart = (function (self, utilities) {
self.Save = function () {}
return self;
})(MFLib.ShoppingCart, MFLib.Utilities);
Patterns In Javascript

Module Pattern : Advantages & Drawbacks
Advantages
1. Modularizes and isolates the code
2. Separates related variables and methods from global
context
3. Establishes control over public and private members

Drawbacks
1. Performance wise not good as Prototype pattern
2. Large code may result into repeated function
3. Not easy to extend
Patterns In Javascript

Some Enhanced Patterns : Revealing Module Pattern
”Enhancement to module pattern in which private
members are also exposed”

Important points :
1. Benefits when we need to have some private
members exposed
2. Private functions remain protected even if public
functions get modified by some chance.
Ex. ShoppingCart.GetTotal = null;
Patterns In Javascript

Some Enhanced Patterns : Revealing Prototype
Pattern
”Combination of Prototype pattern and Module
pattern”

Important points :
1. Better performance than Module Pattern
2. Include prototypes to define the methods in a
module
3. Exposes members through prototype
Patterns In Javascript

Any Question ???
Patterns In Javascript

References and Recommendations


Books
−

Javascript The Good Parts, Douglas Crockford

−

Javascript: The Definitive Guide, David Flanagan

−

Professional Javascript For Developers, Nicholas Zakas



Blogs



Articles



Stack Overflow

Get in touch with :
−

Douglas Crockford

−

Nicholas Zakas

−

Addy Osmani

−

Paul Irish
Patterns In Javascript

Thank You !!!

More Related Content

What's hot

How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsRan Mizrahi
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Librariesjeresig
 
Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Jaroslaw Palka
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practicesManav Gupta
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsMohammad Shaker
 
Scalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureScalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureNicholas Zakas
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesDoris Chen
 
Fundamental JavaScript [In Control 2009]
Fundamental JavaScript [In Control 2009]Fundamental JavaScript [In Control 2009]
Fundamental JavaScript [In Control 2009]Aaron Gustafson
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testingVikas Thange
 
Javascript and Jquery Best practices
Javascript and Jquery Best practicesJavascript and Jquery Best practices
Javascript and Jquery Best practicesSultan Khan
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
A Dexterity Intro for Recovering Archetypes Addicts
A Dexterity Intro for Recovering Archetypes AddictsA Dexterity Intro for Recovering Archetypes Addicts
A Dexterity Intro for Recovering Archetypes AddictsDavid Glick
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Addy Osmani
 
JavaScript Library Overview
JavaScript Library OverviewJavaScript Library Overview
JavaScript Library Overviewjeresig
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayChamnap Chhorn
 
Javascript fundamentals and not
Javascript fundamentals and notJavascript fundamentals and not
Javascript fundamentals and notSalvatore Fazio
 

What's hot (20)

How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design Patterns
 
JavaScript Patterns
JavaScript PatternsJavaScript Patterns
JavaScript Patterns
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
 
Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practices
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
 
Scalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureScalable JavaScript Application Architecture
Scalable JavaScript Application Architecture
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 
Fundamental JavaScript [In Control 2009]
Fundamental JavaScript [In Control 2009]Fundamental JavaScript [In Control 2009]
Fundamental JavaScript [In Control 2009]
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testing
 
Javascript and Jquery Best practices
Javascript and Jquery Best practicesJavascript and Jquery Best practices
Javascript and Jquery Best practices
 
Django design-patterns
Django design-patternsDjango design-patterns
Django design-patterns
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
A Dexterity Intro for Recovering Archetypes Addicts
A Dexterity Intro for Recovering Archetypes AddictsA Dexterity Intro for Recovering Archetypes Addicts
A Dexterity Intro for Recovering Archetypes Addicts
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
JavaScript Library Overview
JavaScript Library OverviewJavaScript Library Overview
JavaScript Library Overview
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
Javascript fundamentals and not
Javascript fundamentals and notJavascript fundamentals and not
Javascript fundamentals and not
 

Similar to Patterns In-Javascript

Javascript design patterns
Javascript design patternsJavascript design patterns
Javascript design patternsGomathiNayagam S
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 
P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Design pattern in an expressive language java script
Design pattern in an expressive language java scriptDesign pattern in an expressive language java script
Design pattern in an expressive language java scriptAmit Thakkar
 
Knockout mvvm-m4-slides
Knockout mvvm-m4-slidesKnockout mvvm-m4-slides
Knockout mvvm-m4-slidesMasterCode.vn
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design PatternsLilia Sfaxi
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptxrani marri
 
Building Scalable JavaScript Apps
Building Scalable JavaScript AppsBuilding Scalable JavaScript Apps
Building Scalable JavaScript AppsGil Fink
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
jquery summit presentation for large scale javascript applications
jquery summit  presentation for large scale javascript applicationsjquery summit  presentation for large scale javascript applications
jquery summit presentation for large scale javascript applicationsDivyanshGupta922023
 
C#.net, C Sharp.Net Online Training Course Content
C#.net, C Sharp.Net Online Training Course ContentC#.net, C Sharp.Net Online Training Course Content
C#.net, C Sharp.Net Online Training Course ContentSVRTechnologies
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patternsamitarcade
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++smumbahelp
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019Paulo Clavijo
 
Java modulesystem
Java modulesystemJava modulesystem
Java modulesystemMarc Kassis
 

Similar to Patterns In-Javascript (20)

Javascript design patterns
Javascript design patternsJavascript design patterns
Javascript design patterns
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Design pattern in an expressive language java script
Design pattern in an expressive language java scriptDesign pattern in an expressive language java script
Design pattern in an expressive language java script
 
Knockout mvvm-m4-slides
Knockout mvvm-m4-slidesKnockout mvvm-m4-slides
Knockout mvvm-m4-slides
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
 
Building Scalable JavaScript Apps
Building Scalable JavaScript AppsBuilding Scalable JavaScript Apps
Building Scalable JavaScript Apps
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
jquery summit presentation for large scale javascript applications
jquery summit  presentation for large scale javascript applicationsjquery summit  presentation for large scale javascript applications
jquery summit presentation for large scale javascript applications
 
C#.net, C Sharp.Net Online Training Course Content
C#.net, C Sharp.Net Online Training Course ContentC#.net, C Sharp.Net Online Training Course Content
C#.net, C Sharp.Net Online Training Course Content
 
Design pattern
Design patternDesign pattern
Design pattern
 
Design Patterns Training From myTectra in Bangalore
Design Patterns Training From myTectra in BangaloreDesign Patterns Training From myTectra in Bangalore
Design Patterns Training From myTectra in Bangalore
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
 
JavaFX in Action Part I
JavaFX in Action Part IJavaFX in Action Part I
JavaFX in Action Part I
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
 
DP PPTS by BK.pptx
DP PPTS by BK.pptxDP PPTS by BK.pptx
DP PPTS by BK.pptx
 
Mini-Training: Javascript Patterns
Mini-Training: Javascript PatternsMini-Training: Javascript Patterns
Mini-Training: Javascript Patterns
 
Java modulesystem
Java modulesystemJava modulesystem
Java modulesystem
 

More from Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 
Angular In Depth
Angular In DepthAngular In Depth
Angular In Depth
 

Recently uploaded

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

Patterns In-Javascript

  • 1. Patterns in Javascript Prototype & Module Patterns Presentation By : Ashutosh Mahto Mindfire Solutions
  • 2. Patterns In Javascript Agenda to Discuss 1. Why to concern about design patterns in Javascript? 2. Before we begin what needs to be known? 3. Prototype Pattern – Structure, Advantages & Drawbacks 4. Module Pattern – Structure, Advantages & Drawbacks 5. Some Enhanced Patterns – Revealing Module Pattern Revealing Prototype Pattern
  • 3. Patterns In Javascript Why a Design Pattern 1. Scattered variables and function 2. Conflicts may arise between variables and methods 3. Difficult to manage when code becomes larger 4. Often result into repeated functions for similar purpose
  • 4. Patterns In Javascript Why a Design Pattern ”A pattern is a reusable solution that can be applied to commonly occurring problems in software design” Benefits of choosing a design pattern 1. Patterns are proven solutions 2. Patterns can be easily reused 3. Patterns can be expressive
  • 5. Patterns In Javascript Before we begin Namespaces - Reduces number of globals and chance of scattered globals - Avoids naming conflicts Ex. var MFLib = MFLib || {}; Closures - Local variables for a function are kept alive after the function has returned - Used widely to differentiate public and private members in javascript Public and Private members in Javascript - Javascript doesn't have special syntax for public and private - Can be implemented using closures
  • 6. Patterns In Javascript Before we begin Prototypes - Prototype is the base of Object Oriented Programming in javascript - Every function contains a prototype object that can be chained through its constructor. var Person= function(name) { this.name = name; } Person.prototype.getName = function() { return this.name; } var student = new Person("Satish");
  • 7. Patterns In Javascript Prototype Pattern : Structure Prototype For Product MFLib.Product = function (name) { this.Id = ''; } MFLib.Product.prototype.setPrice = function (price) {} MFLib.Product.prototype.setDescription = function (description) {}
  • 8. Patterns In Javascript Prototype Pattern : Advantages & Drawbacks Advantages 1. Modularizes and isolates the code 2. Separates related variables and methods from global context 3. Easy to be extended through prototype Drawbacks 1. Restricts access to private members
  • 9. Patterns In Javascript Module Pattern : Structure Shopping Cart Module MFLib.ShoppingCart = (function () { /* Private Variables */ var _basket = []; /* Private Method */ function getShoppingCartTotal() {} return { CreateBasketItem: function () {}, AddItem: function () {}, RemoveItem: function () {}, GetTotal: getShoppingCartTotal }; })();
  • 10. Patterns In Javascript Module Pattern : Structure Shopping Cart Module MFLib.ShoppingCart = (function () { /* Private Variables */ var _basket = []; /* Private Method */ function getShoppingCartTotal() {} return { CreateBasketItem: function () {}, AddItem: function () {}, RemoveItem: function () {}, GetTotal: getShoppingCartTotal }; })();
  • 11. Patterns In Javascript Module Pattern : Important Points Global Import By passing globals as parameters to our anonymous function, we import them into our code MFLib.ShoppingCart = (function ($,window,document,undefined) { })(jQuery,window,document,undefined); Augmentation MFLib.ShoppingCart = (function (self, utilities) { self.Save = function () {} return self; })(MFLib.ShoppingCart, MFLib.Utilities);
  • 12. Patterns In Javascript Module Pattern : Advantages & Drawbacks Advantages 1. Modularizes and isolates the code 2. Separates related variables and methods from global context 3. Establishes control over public and private members Drawbacks 1. Performance wise not good as Prototype pattern 2. Large code may result into repeated function 3. Not easy to extend
  • 13. Patterns In Javascript Some Enhanced Patterns : Revealing Module Pattern ”Enhancement to module pattern in which private members are also exposed” Important points : 1. Benefits when we need to have some private members exposed 2. Private functions remain protected even if public functions get modified by some chance. Ex. ShoppingCart.GetTotal = null;
  • 14. Patterns In Javascript Some Enhanced Patterns : Revealing Prototype Pattern ”Combination of Prototype pattern and Module pattern” Important points : 1. Better performance than Module Pattern 2. Include prototypes to define the methods in a module 3. Exposes members through prototype
  • 16. Patterns In Javascript References and Recommendations  Books − Javascript The Good Parts, Douglas Crockford − Javascript: The Definitive Guide, David Flanagan − Professional Javascript For Developers, Nicholas Zakas  Blogs  Articles  Stack Overflow Get in touch with : − Douglas Crockford − Nicholas Zakas − Addy Osmani − Paul Irish

Editor's Notes

  1. Every developer starts his journey with C, and in most of the cases till he reaches upto web development he gains sufficient knowledge in C, C++, C# or may be Java at least. And here starts the problem. When it comes to javascript most of us usually ignore the basics of javascript assuming its the same, except few array declaration, weak typing etc. So this seminar is not intended to touch the basic declaration, initialization concepts but something additional to that.