Programming: JavaScript

Patch Category: 
Programming
Patch Name: 
JavaScript
Patch First Appeared in Version: 
10.4 (Tiger)
Patch Execution Mode: 
Processor
Patch Description: 

Executes JavaScript code.

Ordered vs. Unordered Structures

Depending on how you allocate structures, they will be either ordered arrays, or unordered dictionaries.

This code

   result.Lines = new Array();
   result.Lines[0] = 'foo';
   result.Lines[1] = 'foo';

results in an ordered array, where element [0] is guaranteed to appear prior to element [1].

Whereas

   result.Lines = new Object();

results in an unordered array, where elements [0] and [1] could appear in any order.

For example, the following code generates a line strip structure representing a circle:

function (__structure Lines) main(__number Segments)
{
   var result = new Object();
   result.Lines = new Array();
   for(i=0;i<Segments;++i)
   {
      result.Lines[i] = new Object();
      result.Lines[i].X = Math.cos(i/Segments * 2 * 3.14)/2.0;
      result.Lines[i].Y = Math.sin(i/Segments * 2 * 3.14)/2.0;
   }
   return result;
}

Since result.Lines is allocated using new Array(), the resulting structure is ordered. If result.Lines were allocated using new Object() instead (but the rest of the code remained the same), the resulting structure would be unordered, and rather than a line strip structure representing a circle, you'd end up with a bunch of pseudorandom circular chords.

Patch Inputs: 

(user-defined)

Patch Outputs: 

(user-defined)

Patch Inspector Settings: 

Code Editor