Command

Command

Command

Constructor

new Command(context, execute, executeArguments, undo, undoArguments)

Undoable command

Parameters:
Name Type Description
context Object

context of the command

execute function

function for action

executeArguments Object

context of the command

undo function

function for undo

undoArguments Object

context of the command

Source:
Tutorials:
Example
const a = { sum: 0 };

const command = new Command(
  (subject, inc) => subject.sum += inc,
  (subject, inc) => subject.sum -= inc,
  [a, 1]
);

command.execute();
// a.sum = 1

command.undo();
// a.sum = 0

Methods

(private) _apply(method, argsopt) → {mixed}

Apply the given function with arguments in the command context.

Parameters:
Name Type Attributes Default Description
method function

Function to call

args Array <optional>
[]

Arguments to apply on

Source:
Returns:
Type
mixed

execute() → {mixed}

Execute the command function with the given arguments

Source:
Returns:
Type
mixed

undo() → {mixed}

Undo the command if possible.

Source:
See:
Throws:

Raises an exception if the command is not undoable

Type
Error
Returns:
Type
mixed

undoable() → {Boolean}

Is the command undoable.

Source:
Returns:

True if undoable, false otherwise

Type
Boolean