Skip to content

Instantly share code, notes, and snippets.

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