views:

26

answers:

0

Hi, I have the following object model I want to get working - have been banging my head against the clone bit! Is this possible? I can't seem to get access to the SubClass constructor from within the baseclass without hard coding it.

The code is for a queue of arbitrary operations that all share a lot of common functionality; new operations are created as subclasses that merely override the action() method for operation specific functionality.

Is this possible without overriding the clone() method in each subclass?

/*
Application specific subclass:
*/
function SubClass_Operation1()
{
  BaseOperationClass.apply(this, arguments); // Call inherited constructor

  this.action = function()
  {
    this.manager.addToQueue(this.clone({changeValue:1}));
    this.manager.addToQueue(this.clone({changeValue:2}));
    this.manager.addToQueue(this.clone({changeValue:3}));
  }
}
SubClass.prototype = new BaseOperationClass;

// More of these defined for each operation
...

/*
Base class containing all common functionality
*/
function BaseOperationClass(manager)
{
  this.manager = manager;

  // Placeholder for virtual action() function
  this.action = function()
  {
  }

  this.clone = function(mods)
  {
    var res = ?????? // Should create exact copy of current SubClass

    res.applyModifications(mods);
  }

  this.applyModifications(mods)
  {
    ...
  }

  // Lots of common functionality 
  ...
}