Created
August 30, 2023 09:15
-
-
Save AnandPilania/882048cb71989a17e7abfec42c03ec04 to your computer and use it in GitHub Desktop.
Extensible VanillaJS
This file contains hidden or 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="myApp.js"></script> | |
| <script src="settings.js"></script> | |
| <script src="utilities.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or 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
| 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; | |
| })(); |
This file contains hidden or 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
| (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); | |
| })(); |
This file contains hidden or 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
| (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