Skip to content

Instantly share code, notes, and snippets.

@AnandPilania
Created August 30, 2023 09:15
Show Gist options
  • Select an option

  • Save AnandPilania/882048cb71989a17e7abfec42c03ec04 to your computer and use it in GitHub Desktop.

Select an option

Save AnandPilania/882048cb71989a17e7abfec42c03ec04 to your computer and use it in GitHub Desktop.
Extensible VanillaJS
<!DOCTYPE html>
<html>
<head>
<title>myApp.js</title>
</head>
<body>
<script src="myApp.js"></script>
<script src="settings.js"></script>
<script src="utilities.js"></script>
</body>
</html>
var myApp = (function () {
'use strict';
var methods = {};
/**
* @param {String} name The new method name
* @param {Function} fn The new method
*/
methods.extend = function (name, fn) {
methods[name] = fn;
};
return methods;
})();
(function () {
'use strict';
var settings = {
debug: false
};
/**
* @param {String} key The setting key
* @param {*} val The new value
*/
var setting = function (key, val) {
if (!(key in settings)) return;
settings[key] = val;
};
/**
* @param {String} key The setting key (optional)
* @return {*} The setting or object of settings
*/
var getSettings = function (key) {
if (key) {
return settings[key];
}
return Object.assign({}, settings);
};
// Extend myApp
myApp.extend('setting', setting);
myApp.extend('getSettings', getSettings);
})();
(function () {
'use strict';
/**
* @param {String} msg The message to log
*/
var log = function (msg) {
if (!myApp.getSettings('debug')) return;
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