ejohn-classjs

0.0.2 • Public • Published

ejohn-classjs

Introduction:

See http://ejohn.org/blog/simple-javascript-inheritance/

Usage:

Access Via Server:

(function main( Class ){
    var SuperClass = Class.extend({
    init : function(){
        //set some variables
        this.setString('Hello World');
    },
    getString : function(){
        //get variable
        return this.class_string;
    },
    setString : function( new_value ){
        this.class_string = new_value;
    }
    }),
    SubClass = SuperClass.extend({
    init : function( prefix ){
        this.prefix = prefix;
        //access super class
        this._super();
        
    },
    setString : function( new_value ){
        //override
        this._super( this.prefix + new_value );
    }
    }),
    super_obj = new SuperClass(),
    sub_obj = new SubClass( 'sub::' );
    console.log('super::',super_obj.getString());
    console.log('sub::',sub_obj.getString());
})( 
    require('ejohn-classjs')
);  

Access Via Client:

(function main( document ){
  require( 'ejohn-classjs', function( Class ){
    var SuperClass = Class.extend(
      init : function(){
        //set some variables
        this.setString('Hello World');
      },
      getString : function(){
        //get variable
        return this.class_string;
      },
      setString : function( new_value ){
        this.class_string = new_value;
      }
    ),
    SubClass = SuperClass.extend(
      init : function( prefix ){
        this.prefix = prefix;
        //access super class
        this._super();
        
      },
      setString : function( new_value ){
        //override
        this._super( this.prefix + new_value );
      }
    ),
    super_obj = new SuperClass(),
    sub_obj = new SubClass( 'sub::' );
  });
})( document );  

Create Class to be Accessed Both:

(function main( ){
  var SuperClass = Class.extend(
    init : function(){
      //set some variables
      this.setString('Hello World');
    },
    getString : function(){
      //get variable
      return this.class_string;
    },
    setString : function( new_value ){
      this.class_string = new_value;
    }
  ),
  SubClass = SuperClass.extend(
    init : function( prefix ){
      this.prefix = prefix;
      //access super class
      this._super();
      
    },
    setString : function( new_value ){
      //override
      this._super( this.prefix + new_value );
    }
  )
  ;
  if( typeof exports === 'undefined' ){
    return {
      SuperClass : SuperClass,
      SubClass : SubClass
    }    
  }else{
    exports.SuperClass = SuperClass;
    exports.SubClass = SubClass;
  }
})( );  

Readme

Keywords

none

Package Sidebar

Install

npm i ejohn-classjs

Weekly Downloads

4

Version

0.0.2

License

MIT

Last publish

Collaborators

  • borrey