Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var ModuleCore = {
  
    // add ModuleCore prototype functions to the new sub module as well as pass in the sub-modules properties and functions and return to 
    // the sub-module so the sub-modules prototype will have CoreModules properties and function and its own properties and function merged
    // into its prototype
    createModule: function(moduleObject){
        var moduleCoreInstance = Object.create(this);
        // add sub-modules properties and functions to CoreModule 
        Object.keys(moduleObject).forEach(function(key){
            moduleCoreInstance[key] = moduleObject[key];
        });
        return moduleCoreInstance;
    },
  
  
    registerSidebar: function(){
        console.log('registerSidebar(sidebar options for module '+this.name+')');
    }
};
var bookmarksModule = ModuleCore.createModule({
    name: 'bookmarks',
    init: function(){
        console.log('bookmarksModule init() function ran from App object '+this.name+')');
    }
});
(function() {
  function App() {
    this.settings = {};
    this.modules = {};
  };
  App.prototype = {
    
    // run registerModules
    init: function(bookmarksModule) {
      
      console.log('App.init() function ran');
      
      // call App.registerModule() and pass in bookmarksModule module name
      this.registerModule('bookmarksModule', bookmarksModule);
    },
    
    // this will create a new instance of "moduleName" and add it to this.modules[moduleName] object
    // for example I am hardcoding the module name as theres 1 module now
    registerModule: function(moduleName, moduleClass) {
      
      // create new instance of module
      this.modules[moduleName] = new bookmarksModule();
      //this.modules[moduleName] = moduleClass;
      
      // run moduleName.init() function from the main App 
      this.modules[moduleName].init();
      
      console.log('App.registerModulemoduleName) function ran and created new instance of moduleName object');
    },
    
    // itterate this.modules object and call this.initModules() function on each module object
    registerModules: function() {
      console.log('App.registerModules() function ran');
    }
  };
  
  window.App = App;
})();
// create new instance of App
var App = new App();
App.init(bookmarksModule);
// call App.registerModule() and pass in bookmarksModule module name
App.registerModule('bookmarksModule', bookmarksModule);
//var moduleTest = new bookmarksModule();
// show bookmarksModule.name property
//console.log(moduleTest.name);
// run bookmarksModule.registerSidebar() function which is inherited from the ModuleCore object instance
//moduleTest.registerSidebar();
// bookmarksModule.init2 = function(){
//   console.log('bookmarksModule init() function ran from ModuleCore module '+this.name+')');
// };
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
jasondavispro
0viewers