Open
Description
Given:
/**
* Create a new precondition.
*
* @param {Object} parameter the parameter value
* @param {String} name the parameter name
* @return {ObjectPreconditions} the precondition
*/
Preconditions.requireThat = function(parameter, name)
{
ObjectPreconditions.instanceOf(name, "name", String);
if (Utilities.instanceOf(parameter, String))
return new StringPreconditions(parameter, name);
if (Array.isArray(parameter))
return new ArrayPreconditions(parameter, name);
return new ObjectPreconditions(parameter, name);
};
where ArrayPreconditions
and StringPreconditions
both subclass ObjectPreconditions
as follows:
/**
* @class ObjectPreconditions
*/
function ObjectPreconditions(parameter, name)
{
...
}
/**
* @class StringPreconditions
* @extends ObjectPreconditions
*/
function StringPreconditions(parameter, name)
{
...
}
StringPreconditions.prototype = Object.create(ObjectPreconditions.prototype);
StringPreconditions.prototype.constructor = ObjectPreconditions;
I am expecting to be able to return ObjectPreconditions
without having to specify every possible subclass. From the end-user's point of view, the subclasses are an implementation detail.