Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.