Skip to content

Instantly share code, notes, and snippets.

@pborissow
Last active March 16, 2021 21:05
Show Gist options
  • Save pborissow/491d7ec867dcd12c5e6fa1c40f4475b2 to your computer and use it in GitHub Desktop.
Save pborissow/491d7ec867dcd12c5e6fa1c40f4475b2 to your computer and use it in GitHub Desktop.
Old School JavaScript

Introduction

Old school way of creating classes in JavaScript

Example

if(!my) var my={};
if(!my.namespace) my.namespace={};

//******************************************************************************
//**  ClassName
//******************************************************************************
/**
 *   Instances of this class are invoked using a DOM element (el) and a config.
 *   
 *   var myClass = new my.namespace.ClassName(el, {});
 *   myClass.doSomething();
 *
 ******************************************************************************/

my.namespace.ClassName = function(parent, config) {

    var me = this;
    var defaultConfig = {
        style: {}
    };


  //**************************************************************************
  //** Constructor
  //**************************************************************************
    var init = function(){

        var div = document.createElement('div');
        parent.appendChild(div);
        me.el = div;
    };


  //**************************************************************************
  //** doSomething
  //**************************************************************************
  /** An example of a public method
   */
    this.doSomething = function(){

      //call a private method in this class
        doSomething();
    };


  //**************************************************************************
  //** doSomething
  //**************************************************************************
  /** An example of a private method
   */
    var doSomething = function(){

      //call a public method in this class
        me.doSomething();

    };


    init();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment