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

More Related Content

What's hot (20)

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

Viewers also liked (11)

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

Similar to Plugin jQuery, Design Patterns (20)

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

Recently uploaded (20)

PDF
FME in Overdrive: Unleashing the Power of Parallel Processing
Safe Software
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PPTX
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
Sound the Alarm: Detection and Response
VICTOR MAESTRE RAMIREZ
 
PDF
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
Governing Geospatial Data at Scale: Optimizing ArcGIS Online with FME in Envi...
Safe Software
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
FME in Overdrive: Unleashing the Power of Parallel Processing
Safe Software
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Sound the Alarm: Detection and Response
VICTOR MAESTRE RAMIREZ
 
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
Governing Geospatial Data at Scale: Optimizing ArcGIS Online with FME in Envi...
Safe Software
 
Wondershare Filmora Crack Free Download 2025
josanj305
 

Plugin jQuery, Design Patterns