fork download
  1. /*
  2.  * Project:
  3.  * Description:
  4.  * Author:
  5.  * License:
  6.  */
  7.  
  8.  
  9. // the semi-colon before function invocation is a safety net against concatenated
  10. // scripts and/or other plugins which may not be closed properly.
  11. ;(function ( $, window, document, undefined ) {
  12.  
  13. // undefined is used here as the undefined global variable in ECMAScript 3 is
  14. // mutable (ie. it can be changed by someone else). undefined isn't really being
  15. // passed in so we can ensure the value of it is truly undefined. In ES5, undefined
  16. // can no longer be modified.
  17.  
  18. // window and document are passed through as local variables rather than globals
  19. // as this (slightly) quickens the resolution process and can be more efficiently
  20. // minified (especially when both are regularly referenced in your plugin).
  21.  
  22. // Create the defaults once
  23. var pluginName = 'defaultPluginName',
  24. defaults = {
  25. propertyName: "value"
  26. };
  27.  
  28. // The actual plugin constructor
  29. function Plugin( element, options ) {
  30. this.element = element;
  31.  
  32. // jQuery has an extend method which merges the contents of two or
  33. // more objects, storing the result in the first object. The first object
  34. // is generally empty as we don't want to alter the default options for
  35. // future instances of the plugin
  36. this.options = $.extend( {}, defaults, options) ;
  37.  
  38. this._defaults = defaults;
  39. this._name = pluginName;
  40.  
  41. this.init();
  42.  
  43. // real code
  44. encapsulatedFunction();
  45. $(this.element).click(function() {
  46. alert('pushed');
  47. });
  48.  
  49. }
  50.  
  51. // an encapsulated function for calling in Plugin
  52. encapsulatedFunction = function() {
  53. console.log('encapsulatedFunction');
  54. };
  55.  
  56. Plugin.prototype.init = function () {
  57. // Place initialization logic here
  58. // You already have access to the DOM element and the options via the instance,
  59. // e.g., this.element and this.options
  60.  
  61. console.log(this.element);
  62. console.log(this.options);
  63. };
  64.  
  65. // A really lightweight plugin wrapper around the constructor,
  66. // preventing against multiple instantiations
  67. $.fn[pluginName] = function ( options ) {
  68. return this.each(function () {
  69. if (!$.data(this, 'plugin_' + pluginName)) {
  70. $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
  71. }
  72. });
  73. }
  74.  
  75. })(jQuery, window, document);
  76.  
Runtime error #stdin #stdout 0.3s 213248KB
stdin
Standard input is empty
stdout
Standard output is empty