Last active
March 16, 2022 20:00
-
-
Save cferdinandi/9aab2f7b3accdf73e7ea888632bc2daa to your computer and use it in GitHub Desktop.
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
// The core app code | |
var myApp = (function () { | |
'use strict'; | |
// Create a public methods object | |
var methods = {}; | |
/** | |
* Extend the public methods object | |
* @param {String} name The new method name | |
* @param {Function} fn The new method | |
*/ | |
methods.extend = function (name, fn) { | |
methods[name] = fn; | |
}; | |
// Return public methods object | |
return methods; | |
})(); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>myApp.js</title> | |
</head> | |
<body> | |
<script src="core.js"></script> | |
<script src="settings.js"></script> | |
<script src="utilities.js"></script> | |
</body> | |
</html> |
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
// Settings functionality | |
(function () { | |
'use strict'; | |
// Create app settings | |
var settings = { | |
debug: false | |
}; | |
/** | |
* Update the settings object | |
* @param {String} key The setting key | |
* @param {*} val The new value | |
*/ | |
var setting = function (key, val) { | |
// if the setting doesn't exist, bail | |
if (!(key in settings)) return; | |
// Update the settings | |
settings[key] = val; | |
}; | |
/** | |
* Get settings | |
* @param {String} key The setting key (optional) | |
* @return {*} The setting or object of settings | |
*/ | |
var getSettings = function (key) { | |
// If there's a key, get a specific setting | |
if (key) { | |
return settings[key]; | |
} | |
// Otherwise return the whole settings object | |
return Object.assign({}, settings); | |
}; | |
// Extend myApp | |
myApp.extend('setting', setting); | |
myApp.extend('getSettings', getSettings); | |
})(); |
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
// Utility functions | |
(function () { | |
'use strict'; | |
/** | |
* Log a message to the console | |
* @param {String} msg The message to log | |
*/ | |
var log = function (msg) { | |
// If not in debug mode, do nothing | |
if (!myApp.getSettings('debug')) return; | |
// Log a message to the console | |
console.log(msg); | |
}; | |
// Extend myApp | |
myApp.extend('log', log); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment