Skip to content

Instantly share code, notes, and snippets.

@mkeneqa
Last active November 8, 2019 09:06
Show Gist options
  • Save mkeneqa/b374efd989f3b54ba35d to your computer and use it in GitHub Desktop.
Save mkeneqa/b374efd989f3b54ba35d to your computer and use it in GitHub Desktop.
Example on how to share JavaScript functions across multiple files with as long as both files are loaded in on the page.
/*---------------------------*/
//JSFILE 1 - file that contains all namespace object and functions
(function(){
var main = window.main || {}
var myNewObjectPage = main.myNewObjectPage = {
markedInvoicesCollection: [], //global array to be used anywhere within the myNewObjectPage
init: function()
{
//all event handlers go here and call internal functions from here
$('#btn_add').live('click',function(){
myNewObjectPage.sayHello();
});
},
sayHello: function ()
{
console.log('Hello World!');
},
letsChat:function()
{
console.log('Lets Chat!');
}
};
$(document).ready(function(){
//initilize object
main.myNewObjectPage.init();
});
})(); // end of function
/*---------------------------*/
//JSFILE 2 - access the object in JSFile 1 using the myCool namespace
main.myNewObjectPage.sayHello //console output: Hello World
main.myNewObjectPage.letsChat //console output: Hey Lets Chat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment