Inheritance jQuery plugin

    Surely, many developers using jQuery encounter inheritance problems in large projects using OOP. Since I myself had recently been puzzled by a similar problem, I decided to write a plug-in for jQuery that helps in this difficult task. Some ideas are borrowed from Base2.

    How to use


    $.inherit([base], methods, [statical])

    * This source code was highlighted with Source Code Highlighter.

    Where:
    • base - the base type (if any)
    • methods - overlapping and new methods
    • statical - static properties and methods

    "Magic" methods and properties


    In addition, there are several "magic" methods:
    • __constructor - method called when instantiating
    • __base - a method that allows calling the same method of the base type
    • this .__ self - this way you can access static properties and methods from within

    Example


    // Базовый тип
    var A = $.inherit(
      {
        __constructor : function(property) {
          this.property = property;
        },
        getProperty : function() {
          return this.property;
        },
        getStaticProperty : function() {
          return this.__self.getStaticProperty();
        }
      },
      {
        staticProperty : 'static property of A',
        getStaticProperty : function() {
          return this.staticProperty;
        }
      });

    // Производный тип от А
    var B = $.inherit(
      A,
      {
        getProperty : function() {
          return this.__base() + ' in B';
        }
      },
      {
        staticProperty : 'static property of B',   
      });

    var instance = new B('value');
    alert(instance.getProperty());
    alert(instance.getStaticProperty());
    alert(B.staticProperty);

    * This source code was highlighted with Source Code Highlighter.
    The plugin itself can be downloaded from here .

    Also popular now: