JS Class / Class.js

https://bitbucket.org/xexsus/js-class/src/2e5718337080eef166d1693261279d04c79761f8/Class.js?fileviewer=file-view-default

by Yury

JavaScript

//JS Class / Class.js
//https://bitbucket.org/xexsus/js-class/src/2e5718337080eef166d1693261279d04c79761f8/Class.js?fileviewer=file-view-default

var Class = {

  extend: function(Child, Parent)
  {
    var F = function() { };
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.superclass = Parent.prototype;
  },
  //~

  mixin: function(dst, src)
  {
    var tobj = {};
    for(var x in src){
      if((typeof tobj[x] == "undefined") || (tobj[x] != src[x])){
        dst[x] = src[x];
      }
    }
    // for IE
    if(document.all && !document.isOpera && src!=null){
      var p = src.toString;
      if((typeof p)=="function" && p!=dst.toString && p!=tobj.toString && p!="\nfunction toString() {\n    [native code]\n}\n"){
        dst.toString = src.toString;
      }
    }
  }
  //~

};
//~



/**
 * Example
 */
var /*abstract*/ Base = function(options){

  this.setDefaultOptions({});
  this.setOptions(options);

};
Class.mixin(Base.prototype, {

  setOptions: function(options)
  {
    Class.mixin(this.options, options);
  },
  //~

  setDefaultOptions: function(options)
  {
    if(this.options===undefined) this.options={};

    Class.mixin(options, this.options);
    this.options = options;
  }
  //~

});
//~



/**
 * Example
 */
var FormWorker = function(form, options){ // constructor

  // options
  this.setDefaultOptions({
    width: null,
    height: null,
    title: null
  });

  // parrent constructor
  FormWorker.superclass.constructor.apply(this, [options]);

  // class properties
  this._form = null;

  // constructor action
  this.setForm(form);

};
Class.extend(FormWorker, Base); // inheritance
Class.mixin(FormWorker.prototype, {

  setOptions: function(options) // overriding
  {
    FormWorker.superclass.setOptions.apply(this, arguments); // parrent
    // ...
  },
  //~

  setForm: function(form)
  {
    // ...
    this._form = form;
  },
  //~

  _getFormData: function() // protected
 ...