Created
December 1, 2016 16:10
-
-
Save yan-kisen/e1195f0ebf43cec6297942307e254bc0 to your computer and use it in GitHub Desktop.
Creating a global Js Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( function( $ ) { | |
// Create a jQuery Object. | |
$.fn.myObject = function() { | |
var target = this; // Store the target element in this variable (jQuery stuff). | |
// Our stuff. | |
var myObject = { | |
/** | |
* 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; // Return jQuery for chaining. | |
}, | |
/** | |
* 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( myObject ); // Our stuff. | |
console.log( target ); // jQuery stuff. | |
return this; // Return jQuery for chaining. | |
}, | |
}; | |
myObject.__construct(); // Always run these things when the Object is created. | |
return myObject; // return the object so we can access other properties. | |
}; | |
} ( jQuery ) ); | |
// Kick it off.a | |
jQuery().myObject(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment