Composite

Composite

The base class which provides the implementation of the Composite design pattern. A composite object can form a tree-like structure, the nodes or leaves of which are also composites

Constructor

new Composite()

Source:

Members

(readonly) ancestors :Array.<Composite>

The ancestor composites ordered by closest to furthest

Source:
Type:

(readonly) hasChild :boolean

Whether this has one or more child composites

Source:
Type:
  • boolean

(readonly) hasParent :boolean

Whether this has parent composite

Source:
Type:
  • boolean

(readonly) length :number

The number of child composites

Source:
Type:
  • number

(readonly) parent :Composite

The parent composite

Source:
Type:

(readonly) root :Composite

The root of composition

Source:
Type:

Methods

addChild(Cp) → {Composite}

Adds a child composite

Source:
Parameters:
Name Type Description
Cp Composite

The composite to add as a child

Returns:
Type:
Composite

This

addChildren(Cps) → {Composite}

Adds multiple child composites

Source:
Parameters:
Name Type Description
Cps Array.<Composite>

The array of the composites to add

Returns:
Type:
Composite

This

traverse(Fn, Depth, Arg) → {boolean}

Performs tree traversal

Source:
Parameters:
Name Type Default Description
Fn function

The callback that receives every descendant composite as the 1st parameter. If false is returned, the traversal will be aborted. The returned value other than false will be passed to the next traversal call of Fn as the 2nd parameter

Depth number -1

The limit of traversal depth. Negative number means no-limit

Arg mixed null

Additinal argument to pass to Fn as the 2nd parameter.

Returns:
Type:
boolean

true if the traversal is successfully completed. false if the traversal is aborted

verifyChild(Cp) → {boolean|string}

Determines whether the specified composite can be added as a child

Source:
Parameters:
Name Type Description
Cp Composite

The composite which is about to be added

Returns:
Type:
boolean | string

true if Cp is valid. Any type other than true means invalid. If a string is returned, it is shown as an error message in the debug console