Node

Node

Node class

Constructor

new Node(name, data, tree)

Creates an instance of a node. Do not use this constructor yourself. To add nodes, use the add and addChild methods

Parameters:
Name Type Description
name string

The name of the node

data

Node data

tree Tree

The tree to which the node will belong

Source:

Members

children

A getter that returns an array of children of the given node

Source:

data

Data of this node

Source:

name

Name of this node

Source:

parent

Getter to get the parent of this node

Source:

path

Getter to get the path to this node

Source:

tree

The tree to which the node will belong

Source:

Methods

addChild(name, data)

Adds a child to this node

This:
Parameters:
Name Type Description
name string

The name of the node to add

data

The data of the node being added

Source:
Throws:
  • In case this node does not belong to any tree

    Type
    TreeError
  • In case the node already exists

    Type
    TreeError

remove()

Deletes the given node and its children

This:
Source:
Throws:
  • In case this node does not belong to any tree

    Type
    TreeError
  • If this node is the root

    Type
    TreeError

toJSON() → {object}

Returns a node object suitable for storage in JSON format. This method is mainly used by the JSON.stringify function

This:
Source:
Example
node.toJSON(); // { name: "root", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [{ name: "node1", data: {text: "Hello, world!", "otherText": "hoI!" }, children: [{ name: "node2", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [] }] }

traverse(callback)

Calls a callback for each child node of this node

This:
Parameters:
Name Type Description
callback function

A function called for child node of this node The node in the first argument is passed to the function

Source:
Example
node.traverse(node => {
  console.log(node.name);
});