SlideShare a Scribd company logo
jQuery Plugin
Design Patterns
“Deep into JS” Meetup
Constructor
Function
function Person() {}
function Person(name, surname) {!
! ! ! this.name = name;!
! ! ! this.surname = surname;!
}
function Person(name, surname) {!
! ! ! this.name = name;!
! ! ! this.surname = surname;!
! ! ! this.getName = function() {!
! ! ! ! ! ! // privileged method: useful if you need private method, attributes!
! ! ! ! ! ! return this.name;!
! ! ! }!
}
function Person(name, surname) {!
! ! ! this.name = name;!
! ! ! this.surname = surname;!
}!
Person.prototype.getName = function() {!
! ! ! ! return this.name;!
}
var robb = new Person(‘Robert’, ‘Casanova’);!
console.log(robb.getName());
Object.create()
var robb = {

! ! name: ‘Robert’,

! ! surname: ‘Casanova’,!
! ! getName: function() {

! ! ! ! ! return this.name;

! ! }!
}
var gigi = Object.create(robb);!
gigi.name = ‘Giorgio’;!
gigi.surname = ‘Moroder’;!
!
console.log(gigi.getName())
Plugin jQuery
$.fn[‘pluginName’] = function() {

this.each(function(){

$(this).doSomething();

});

}
Simplest jQuery plugin ever
LightWeight
Start Pattern
;(function($, window,document, undefined ){!
//plugin goes here!
})(jQuery, window, document)
Immediately Invoked Function Expression
(IIFE)
var pluginName = “mioPlugin”,!
defaults = {

defaultProperty: ‘defaultValue’

}
Defaults
function Plugin(el, options) {!
! this.el = el;!
this.$el = $(el);!
this.options = $.extend({},defaults, options);!
! ! !
this._defaults = defaults;!
this._name = pluginName;!
!
! ! this.init();!
}
Constructor Function
Plugin.prototype.init = function() {!
! ! ! ! //the initial logic goes here!
}!
Plugin.prototype.someMethod = function() {!
…!
}
Methods
$.fn[pluginName] = function(options) {!
! ! ! return this.each(function(){!
! ! ! ! ! if(!$.data(this, pluginName)) {!
! ! ! ! ! ! $.data(this, pluginName, new Plugin(this, options));!
! ! ! ! ! }!
! ! ! });!
}
Plugin Magic
$(‘#elem’).pluginName({!
!! defaultProperty: ‘value’!
});
Example
DOM-to-Object
Bridge Pattern
var myObject = {!
! ! init: function (options,elem) {!
! ! ! ! …!!
! ! }!
}
Object
init: function (options,elem) {!
! ! this.options = $.extend({}, this.options,options);!
! ! this.el = elem;!
this.$el = $(elem);!
!
this._build();!
!
return this;!
}
Init Function
options: {!
! defaultOption: ‘defaultValue’!
}
Default options
_build: function() {!
! this.$el.html(‘inizialize html here’);!
},!
publicMethod: function() {

this.$el.doSomething();

}
Methods
$.plugin = function(name,object) {!
! ! $.fn[name] = function(options) {!
! ! ! ! return this.each(function(){

! ! ! ! ! ! if(!$.data(this,name)) {

$.data(this,name, Object.create(myObject).init(options,this))
}

})!
! ! }!
} !
$.plugin(“pluginName”, myObject )
Plugin Magic
$(‘#elem’).pluginName({!
!! defaultProperty: ‘value’!
});!
!
var plugin = $(‘#elem’).data(‘pluginName’);!
plugin.publicMethod();
Example
Ad

Recommended

jQuery Plugin Creation
jQuery Plugin Creation
benalman
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
andrewnacin
 
Mojolicious
Mojolicious
Marcos Rebelo
 
Mojolicious - A new hope
Mojolicious - A new hope
Marcus Ramberg
 
Task 1
Task 1
EdiPHP
 
RESTful web services
RESTful web services
Tudor Constantin
 
Mojolicious on Steroids
Mojolicious on Steroids
Tudor Constantin
 
Maintainable JavaScript 2012
Maintainable JavaScript 2012
Nicholas Zakas
 
Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn't
Cosimo Streppone
 
With a Mighty Hammer
With a Mighty Hammer
Ben Scofield
 
jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & Compression
Paul Irish
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro framework
Jeremy Kendall
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
Jeremy Kendall
 
Mojolicious, real-time web framework
Mojolicious, real-time web framework
taggg
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right Reasons
Matt Follett
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
 
Drupal Best Practices
Drupal Best Practices
manugoel2003
 
Developing apps using Perl
Developing apps using Perl
Anatoly Sharifulin
 
Building Cloud Castles
Building Cloud Castles
Ben Scofield
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Django Heresies
Django Heresies
Simon Willison
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
Anatoly Sharifulin
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Dotan Dimet
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
Jeremy Kendall
 
You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011
andrewnacin
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)
andrewnacin
 
Mojo as a_client
Mojo as a_client
Marcus Ramberg
 
Djangocon 2014 angular + django
Djangocon 2014 angular + django
Nina Zakharenko
 
Writing your Third Plugin
Writing your Third Plugin
Justin Ryan
 
An easy guide to Plugin Development
An easy guide to Plugin Development
Shinichi Nishikawa
 

More Related Content

What's hot (20)

Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn't
Cosimo Streppone
 
With a Mighty Hammer
With a Mighty Hammer
Ben Scofield
 
jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & Compression
Paul Irish
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro framework
Jeremy Kendall
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
Jeremy Kendall
 
Mojolicious, real-time web framework
Mojolicious, real-time web framework
taggg
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right Reasons
Matt Follett
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
 
Drupal Best Practices
Drupal Best Practices
manugoel2003
 
Developing apps using Perl
Developing apps using Perl
Anatoly Sharifulin
 
Building Cloud Castles
Building Cloud Castles
Ben Scofield
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Django Heresies
Django Heresies
Simon Willison
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
Anatoly Sharifulin
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Dotan Dimet
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
Jeremy Kendall
 
You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011
andrewnacin
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)
andrewnacin
 
Mojo as a_client
Mojo as a_client
Marcus Ramberg
 
Djangocon 2014 angular + django
Djangocon 2014 angular + django
Nina Zakharenko
 
Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn't
Cosimo Streppone
 
With a Mighty Hammer
With a Mighty Hammer
Ben Scofield
 
jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & Compression
Paul Irish
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro framework
Jeremy Kendall
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
Jeremy Kendall
 
Mojolicious, real-time web framework
Mojolicious, real-time web framework
taggg
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right Reasons
Matt Follett
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
 
Drupal Best Practices
Drupal Best Practices
manugoel2003
 
Building Cloud Castles
Building Cloud Castles
Ben Scofield
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
Anatoly Sharifulin
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Dotan Dimet
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
Jeremy Kendall
 
You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011
andrewnacin
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)
andrewnacin
 
Djangocon 2014 angular + django
Djangocon 2014 angular + django
Nina Zakharenko
 

Viewers also liked (11)

Writing your Third Plugin
Writing your Third Plugin
Justin Ryan
 
An easy guide to Plugin Development
An easy guide to Plugin Development
Shinichi Nishikawa
 
Building an Eclipse plugin to recommend changes to developers
Building an Eclipse plugin to recommend changes to developers
kim.mens
 
Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin Programming
Dougal Campbell
 
Building GPE: What We Learned
Building GPE: What We Learned
rajeevdayal
 
A Simple Plugin Architecture for Wicket
A Simple Plugin Architecture for Wicket
nielsvk
 
Eclipse Overview
Eclipse Overview
Lars Vogel
 
So, you want to be a plugin developer?
So, you want to be a plugin developer?
ylefebvre
 
The Open-source Eclipse Plugin for Force.com Development, Summer ‘14
The Open-source Eclipse Plugin for Force.com Development, Summer ‘14
Salesforce Developers
 
Configuration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL Plugin
Daniel Spilker
 
Creating a Plug-In Architecture
Creating a Plug-In Architecture
ondrejbalas
 
Writing your Third Plugin
Writing your Third Plugin
Justin Ryan
 
An easy guide to Plugin Development
An easy guide to Plugin Development
Shinichi Nishikawa
 
Building an Eclipse plugin to recommend changes to developers
Building an Eclipse plugin to recommend changes to developers
kim.mens
 
Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin Programming
Dougal Campbell
 
Building GPE: What We Learned
Building GPE: What We Learned
rajeevdayal
 
A Simple Plugin Architecture for Wicket
A Simple Plugin Architecture for Wicket
nielsvk
 
Eclipse Overview
Eclipse Overview
Lars Vogel
 
So, you want to be a plugin developer?
So, you want to be a plugin developer?
ylefebvre
 
The Open-source Eclipse Plugin for Force.com Development, Summer ‘14
The Open-source Eclipse Plugin for Force.com Development, Summer ‘14
Salesforce Developers
 
Configuration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL Plugin
Daniel Spilker
 
Creating a Plug-In Architecture
Creating a Plug-In Architecture
ondrejbalas
 
Ad

Similar to Plugin jQuery, Design Patterns (20)

jQuery Namespace Pattern
jQuery Namespace Pattern
Diego Fleury
 
Jquery Plugin
Jquery Plugin
Ravi Mone
 
jQuery Plugin
jQuery Plugin
Elijah Manor
 
What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?
Christophe Porteneuve
 
Bringing classical OOP into JavaScript
Bringing classical OOP into JavaScript
Dmitry Sheiko
 
Jquery plugin development
Jquery plugin development
Faruk Hossen
 
Prototype Framework
Prototype Framework
Julie Iskander
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
Alive Kuo
 
Learn JS concepts by implementing jQuery
Learn JS concepts by implementing jQuery
Wingify Engineering
 
Building a JavaScript Library
Building a JavaScript Library
jeresig
 
Four Ways to add Features to Ext JS
Four Ways to add Features to Ext JS
Shea Frederick
 
Eugene Andruszczenko: jQuery
Eugene Andruszczenko: jQuery
Refresh Events
 
jQuery Presentation - Refresh Events
jQuery Presentation - Refresh Events
Eugene Andruszczenko
 
Download full ebook of Extending jQuery Keith Wood download pdf instant downl...
Download full ebook of Extending jQuery Keith Wood download pdf instant downl...
busicluckesz
 
Javascript Design Patterns
Javascript Design Patterns
Iván Fernández Perea
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
johndaviddalton
 
ClassJS
ClassJS
Michael Barrett
 
OOP and JavaScript
OOP and JavaScript
easelsolutions
 
Javascript Design Patterns
Javascript Design Patterns
Subramanyan Murali
 
Software Design Patterns
Software Design Patterns
Satheesh Sukumaran
 
jQuery Namespace Pattern
jQuery Namespace Pattern
Diego Fleury
 
Jquery Plugin
Jquery Plugin
Ravi Mone
 
What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?
Christophe Porteneuve
 
Bringing classical OOP into JavaScript
Bringing classical OOP into JavaScript
Dmitry Sheiko
 
Jquery plugin development
Jquery plugin development
Faruk Hossen
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
Alive Kuo
 
Learn JS concepts by implementing jQuery
Learn JS concepts by implementing jQuery
Wingify Engineering
 
Building a JavaScript Library
Building a JavaScript Library
jeresig
 
Four Ways to add Features to Ext JS
Four Ways to add Features to Ext JS
Shea Frederick
 
Eugene Andruszczenko: jQuery
Eugene Andruszczenko: jQuery
Refresh Events
 
jQuery Presentation - Refresh Events
jQuery Presentation - Refresh Events
Eugene Andruszczenko
 
Download full ebook of Extending jQuery Keith Wood download pdf instant downl...
Download full ebook of Extending jQuery Keith Wood download pdf instant downl...
busicluckesz
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
johndaviddalton
 
Ad

Recently uploaded (20)

Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
Precisely
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
Precisely
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 

Plugin jQuery, Design Patterns