SLB: Notes – JS: Classes

By Sol in Lab

Requirements

  • Consistent access to base object (SLB global variable)
  • Support for methods/properties
  • Consistent access to utility/helper methods/properties (prefix, context, etc.)
  • Organized structure
  • Extendible – Attach members/data to objects
    • e.g. SLB (base object), SLB.util (utility object), SLB.View (view controller), etc.

Implementation (Options)

  1. Current: Object literals with extend method
    • Define methods in object declaration
    • - Object properties cannot reference self via this (references window object)
      • Less modular code
    • + Simple declarations
  2. Constructor function
    • Objects are instances of base class definition
    • Base constructor defines standard properties/methods (utilities, etc.)
      SLB = new Base();
      • All objects have direct access to standard methods/properties (SLB.View.util.get_prefix();)
      • ? Better than single base object with properties/methods (SLB.View.base.util references SLB.util)
        • A: Yes
    • Will need to wrap initialization and attachment of new members into single method
      • e.g. SLB.attach('View', {data});
      • ? Method name? – currently extend, but may conflict with Class.extend() (Inheritance)
        1. Single method – 2 modes (based on parameters)
          • SLB.extend('View', data) (2 parameters)
            • Add View member to SLB object
            • Create new instance of base class inherited by View object
            • data (object) – Properties/methods for new member
          • SLB.extend(obj) (1 parameter)
            • Extends SLB object and returns new instance
          • + Simple
          • + Single method for both related functionalities
            • One version simply adds it to global object
          • - Confusion: Better to have separate methods for sake of clarity?
            • extend: Create new class inheriting base class
            • attach: Add member to object (creating new class that inherits base class)
    • Structure
      • Class Definitions
        • Base
          • Utility/Helper methods
            • References base object for instance properties (prefix, etc.)
          • Member attachment functionality
          • Standard properties/methods
        • Core
          • Extends Base class
          • Functionality specific to core object
      • Instance Objects
        • Core (SLB)
          • Base instance for all other members to be attached to
          • Members
          • View Controller (SLB.View)
            • Lightbox functionality controller
            • SLB.attach('View', obj class_definition)
              • New View class created (extends Base class)
            • + Access to standard properties/methods without having to “travel” to Core (SLB) object
            • Components (viewers, groups, etc.)
              • Attached via
                SLB.View.Viewer = SLB.View.extend(obj class_definition)