Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active March 16, 2022 20:00
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cferdinandi/9aab2f7b3accdf73e7ea888632bc2daa to your computer and use it in GitHub Desktop.
Save cferdinandi/9aab2f7b3accdf73e7ea888632bc2daa to your computer and use it in GitHub Desktop.
// 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;
})();
<!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>
// 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);
})();
// 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