Skip to main content

JavaScript Node Definition Walkthrough

To construct a node in JavaScript there are a few steps that must be taken to ensure the node is setup correctly.

The first step is to open the std-nodes.js file and scroll to the bottom and add a node declaration.

// Create an object that will hold information about a node.
export function MagnitudeNode() {
this.mag = 0.0;
}

The next step is to define all of the entry points for this node.

export function MagnitudeNode() {
this.mag = 0.0;

// 'calculate' entrypoint
this.calculate = function (ctx, x, y) {
// Save magnitude to instance
this.mag = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));

// Call next node in flow
$ld.lookupCallable(this, "$finished").call(ctx);
}

// 'getMagnitude' entrypoint
this.getMagnitude = function (ctx) {
// return the saved magnitude
return this.mag;
}
}

The next step is to register a function that can be used to construct an instance of this node.

// Register the Magnitude node
export var constructMagnitudeNode = $RegisterClass(MagnitudeNode);

When registering a node, you can see how a special function was used for this purpose - $RegisterClass. This function will perform all steps necessary to expose all entrypoints for a node with the Automate system. The value returned from the $RegisterClass function is a value that can be passed to another special function called $ConstructClass that will use the value to construct an instance of the registered class.

Another thing to note from this construction mechanism is the use of the export keyword. This keyword tells the JavaScript system what functions, variables, and classes should be visible outside of this file. Since the rest of the system needs to be able to construct instances of classes in a JavaScript file, those classes must be exported.

If you have not used JavaScript before, the this variable is a special keyword that represents the instance of an object that was used to call the function.