SlideShare una empresa de Scribd logo
1 de 18
$(“jQuery”).Plugin()
Will Discuss
1. What is jQuery Plugin?
2. Private Scope
3. Using Good Template
4. Understanding Template
5. Plugin Syntax
6. jQuery Prototype
7. Adding Options(Single Argument)
8. Object Literals(Multiple Arguments)
9. Why this.each() ?
10. Steps for Developing Better jQuery plugins
1. What is jQuery Plugin
General definition : Fairly common functions outside of the jQuery
library.
OR
A Collection of javascript/jQuery function intended to achieve a
feature for a given element in a private scope.
2. Private Scope ?
(function($) {
// Shell for your plugin code
//Code Written here will not make conflict with //global Scope
code
})(jQuery);
3. Use a Good Template
/*!
* jQuery plugin. What does it do
*/
(function($) {
$.fn.PlugInName = function(opts) {
// default configuration
var config = $.extend({}, {
opt1: null
}, opts);
// main function
function DoSomething(e) {}
Continued...
...
// initialize every element
this.each(function() {
DoSomething($(this));
});
return this;
};
})(jQuery);
4. Understanding Template
(function($) {
// Shell for your plugin code
})(jQuery);
The safest (only!) way of doing this, is to create a private scope
for the jQuery function. This JavaScript trick ensures that your
plugin will work nicely, even on pages where a person is using
the $ function for non-jQuery purposes:
5. Plugin Syntax
$.fn.pluginname = function() {
// Plugin code
}
$.fn is a shortcut to the jQuery.prototype JavaScript property.
This code can go anywhere in your script, but standard practice is
to put it in a separate JavaScript file named jquery.pluginname.js,
and include it as you’d include any javascript file.
<script src="jquery.pluginname.js"></script>
Now that you have a stand-alone file, you can easily use it in
future projects or share it with the world!
6. What is this jQuery.prototype ?
Prototype is a style of object-oriented programming in which
classes are not present, and behavior reuse (known as
inheritance in class-based languages) is performed via a
process of cloning existing objects that serve as prototypes.
This model can also be known as classless, prototype-oriented or
instance-based programming.
function Test() { this.a = 'a';}
Test.prototype.b = 'b';
var test = new Test();
test.a; // "a", own property
test.b; // "b", inherited property
7. Adding Options
jQuery plugins are an excellent way to produce reusable code, but
to be truly useful, our plugins need to be applicable outside the
context for which we created them: they need to be customizable.
$.fn.highlightOnce = function(color) {
⋮
$(this).css('background-color', color || '#fff47f')
};
//Default plugin call
$(“selector”).highlightOnce();
//Plugin call with param
$(“selector”).highlightOnce('#ccfadb');
8. Object literals Options
$.fn.highlightOnce = function(opt) {
var defaults = { color : '#fff47f', duration : 'fast' };
options = $.extend(defaults, opt);
$(this).css('background-color', options.color);
...
};
//Default plugin call
$(“selector”).highlightOnce(); //default plugin function executed
//Default plugin call with param
$(“selector”).highlightOnce({color: '#ccfadb',duration :
'slow' }); //user passed values executed
9. Why this.each(function()) in jQuery plugins?
this.each(function() {
DoSomething($(this));
});
When you filter elements with a selector ($('.myclass')), it can
match more than only one element.
With each, you iterate over all matched elements and your code is
applied to all of them.
//Can Do return “this”
return this.each(function() {
DoSomething($(this));
});
Continued...
Unless your plugin returns a value, the last line of your plugin
function must
return this;
this ensures method calls can be chained, e.g.
$("selector").yourPlugin().anotherPlugin()....;
Demo Time
10. Steps for Developing Better jQuery Plugins
1.Make it Easy to Use
– In most cases, your plugin should simply work without the
developer having to wade though documentation, set
options or edit your plugin code.
2. Use Suitable Naming and Version Control Numbers
– There are a lot of jQuery plugins. If you’re considering the
name “tab” for your tab-handling plugin, there’s a strong
possibility it’s been used already. That may not always
matter but avoid using names which are ambiguous or
likely to clash.
3.Use a Closure
– (function($) {
// code here can use $ to reference jQuery
})(jQuery);
Continued...
●
4.Set Default Parameters
– $.fn.PlugIn = function(opts) {
// default configuration
var config = $.extend(config, opts);
}
5.Don’t Break the Chain
– Unless your plugin returns a value, the last line of your
plugin function must be:
return this;
6.Document Your Code
– Add concise comments to the top of your plugin
7.Test Your Plugin Thoroughly
Questions
Jquery Plugin

Más contenido relacionado

La actualidad más candente

jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
dmethvin
 
[PyConTW 2013] Write Sublime Text 2 Packages with Python
[PyConTW 2013] Write Sublime Text 2 Packages with Python[PyConTW 2013] Write Sublime Text 2 Packages with Python
[PyConTW 2013] Write Sublime Text 2 Packages with Python
Jenny Liang
 
Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
Yehuda Katz
 

La actualidad más candente (20)

Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
Intro to jQuery
Intro to jQueryIntro to jQuery
Intro to jQuery
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
jQuery
jQueryjQuery
jQuery
 
jQuery Presentation
jQuery PresentationjQuery Presentation
jQuery Presentation
 
Prototype & jQuery
Prototype & jQueryPrototype & jQuery
Prototype & jQuery
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
 
jQuery in 15 minutes
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutes
 
jQuery
jQueryjQuery
jQuery
 
Javascript in Plone
Javascript in PloneJavascript in Plone
Javascript in Plone
 
[PyConTW 2013] Write Sublime Text 2 Packages with Python
[PyConTW 2013] Write Sublime Text 2 Packages with Python[PyConTW 2013] Write Sublime Text 2 Packages with Python
[PyConTW 2013] Write Sublime Text 2 Packages with Python
 
Dojo Confessions
Dojo ConfessionsDojo Confessions
Dojo Confessions
 
NodeJS The edge of Reason - Lille fp#6
NodeJS The edge of Reason - Lille fp#6NodeJS The edge of Reason - Lille fp#6
NodeJS The edge of Reason - Lille fp#6
 
How to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQueryHow to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQuery
 
Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 

Similar a Jquery Plugin

J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
rsnarayanan
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 

Similar a Jquery Plugin (20)

Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
J Query
J QueryJ Query
J Query
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and Geb
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
JavaScript Coding with Class
JavaScript Coding with ClassJavaScript Coding with Class
JavaScript Coding with Class
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksZepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
 
AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
 
Javascript for the c# developer
Javascript for the c# developerJavascript for the c# developer
Javascript for the c# developer
 
Modular programming Using Object in Scala
Modular programming Using Object in ScalaModular programming Using Object in Scala
Modular programming Using Object in Scala
 
Beginning jQuery
Beginning jQueryBeginning jQuery
Beginning jQuery
 
Mastering Test Automation: How To Use Selenium Successfully
Mastering Test Automation: How To Use Selenium SuccessfullyMastering Test Automation: How To Use Selenium Successfully
Mastering Test Automation: How To Use Selenium Successfully
 
jQuery Tips Tricks Trivia
jQuery Tips Tricks TriviajQuery Tips Tricks Trivia
jQuery Tips Tricks Trivia
 
Selenium-Browser-Based-Automated-Testing-for-Grails-Apps
Selenium-Browser-Based-Automated-Testing-for-Grails-AppsSelenium-Browser-Based-Automated-Testing-for-Grails-Apps
Selenium-Browser-Based-Automated-Testing-for-Grails-Apps
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for selenium
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Jquery Plugin

  • 2.
  • 3. Will Discuss 1. What is jQuery Plugin? 2. Private Scope 3. Using Good Template 4. Understanding Template 5. Plugin Syntax 6. jQuery Prototype 7. Adding Options(Single Argument) 8. Object Literals(Multiple Arguments) 9. Why this.each() ? 10. Steps for Developing Better jQuery plugins
  • 4. 1. What is jQuery Plugin General definition : Fairly common functions outside of the jQuery library. OR A Collection of javascript/jQuery function intended to achieve a feature for a given element in a private scope.
  • 5. 2. Private Scope ? (function($) { // Shell for your plugin code //Code Written here will not make conflict with //global Scope code })(jQuery);
  • 6. 3. Use a Good Template /*! * jQuery plugin. What does it do */ (function($) { $.fn.PlugInName = function(opts) { // default configuration var config = $.extend({}, { opt1: null }, opts); // main function function DoSomething(e) {}
  • 7. Continued... ... // initialize every element this.each(function() { DoSomething($(this)); }); return this; }; })(jQuery);
  • 8. 4. Understanding Template (function($) { // Shell for your plugin code })(jQuery); The safest (only!) way of doing this, is to create a private scope for the jQuery function. This JavaScript trick ensures that your plugin will work nicely, even on pages where a person is using the $ function for non-jQuery purposes:
  • 9. 5. Plugin Syntax $.fn.pluginname = function() { // Plugin code } $.fn is a shortcut to the jQuery.prototype JavaScript property. This code can go anywhere in your script, but standard practice is to put it in a separate JavaScript file named jquery.pluginname.js, and include it as you’d include any javascript file. <script src="jquery.pluginname.js"></script> Now that you have a stand-alone file, you can easily use it in future projects or share it with the world!
  • 10. 6. What is this jQuery.prototype ? Prototype is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as classless, prototype-oriented or instance-based programming. function Test() { this.a = 'a';} Test.prototype.b = 'b'; var test = new Test(); test.a; // "a", own property test.b; // "b", inherited property
  • 11. 7. Adding Options jQuery plugins are an excellent way to produce reusable code, but to be truly useful, our plugins need to be applicable outside the context for which we created them: they need to be customizable. $.fn.highlightOnce = function(color) { ⋮ $(this).css('background-color', color || '#fff47f') }; //Default plugin call $(“selector”).highlightOnce(); //Plugin call with param $(“selector”).highlightOnce('#ccfadb');
  • 12. 8. Object literals Options $.fn.highlightOnce = function(opt) { var defaults = { color : '#fff47f', duration : 'fast' }; options = $.extend(defaults, opt); $(this).css('background-color', options.color); ... }; //Default plugin call $(“selector”).highlightOnce(); //default plugin function executed //Default plugin call with param $(“selector”).highlightOnce({color: '#ccfadb',duration : 'slow' }); //user passed values executed
  • 13. 9. Why this.each(function()) in jQuery plugins? this.each(function() { DoSomething($(this)); }); When you filter elements with a selector ($('.myclass')), it can match more than only one element. With each, you iterate over all matched elements and your code is applied to all of them. //Can Do return “this” return this.each(function() { DoSomething($(this)); });
  • 14. Continued... Unless your plugin returns a value, the last line of your plugin function must return this; this ensures method calls can be chained, e.g. $("selector").yourPlugin().anotherPlugin()....; Demo Time
  • 15. 10. Steps for Developing Better jQuery Plugins 1.Make it Easy to Use – In most cases, your plugin should simply work without the developer having to wade though documentation, set options or edit your plugin code. 2. Use Suitable Naming and Version Control Numbers – There are a lot of jQuery plugins. If you’re considering the name “tab” for your tab-handling plugin, there’s a strong possibility it’s been used already. That may not always matter but avoid using names which are ambiguous or likely to clash. 3.Use a Closure – (function($) { // code here can use $ to reference jQuery })(jQuery);
  • 16. Continued... ● 4.Set Default Parameters – $.fn.PlugIn = function(opts) { // default configuration var config = $.extend(config, opts); } 5.Don’t Break the Chain – Unless your plugin returns a value, the last line of your plugin function must be: return this; 6.Document Your Code – Add concise comments to the top of your plugin 7.Test Your Plugin Thoroughly