Skip to content

Instantly share code, notes, and snippets.

@gregrickaby
Forked from aubreypwd/jsClass.jQuery.js
Created January 11, 2016 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregrickaby/a20da5b4584dbccf2329 to your computer and use it in GitHub Desktop.
Save gregrickaby/a20da5b4584dbccf2329 to your computer and use it in GitHub Desktop.
How to create a Js application as a jQuery plugin
( function( $ ) {
// Create a jQuery Object.
$.fn.myObject = function() {
var target = this; // Store the target element in this variable (jQuery stuff).
// Our stuff.
this.object = {
/**
* Run the Construct every time we create a new instance.
*
* @return {Object} This object.
*/
__construct: function() {
this.loaded(); // Show that this is loaded, remove when you're sure it's loading in your project.
return this;
},
/**
* Loaded notice.
*
* This allows us to add this easily to a project and to give us feedback that it's loading properly.
*
* @return {Object} This object.
*/
loaded: function() {
console.log( 'Loaded myObject...' );
console.log( this ); // Our stuff.
console.log( target ); // jQuery stuff.
return this;
},
};
this.object.__construct(); // Always run these things when the Object is created.
return this.object; // return the object so we can access other properties.
};
} ( jQuery ) );
// Kick it off.
jQuery().myObject();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment