Javascript and Data
Javascript and Data
Advanced interactions
on complex data in Max
using Javascript
Keep in mind that Javascript is continuously under development and that older
resources may not cover recent added techniques. At the same time, the js
object works with a slightly outdated version of Javascript that does not include
latest technologies.
•jvkr
1. introduction
Javascript can be incorporated in Max in the
following way; the first step is to write code,
the second step is to expose that code to
Max with the js object, and finally the
functions in the code are called which then
produce their result.
In this example a single function is defined.
As soon as the js object receives a bang,
the function carrying that name is called and
executed. This results in a line being printed
(or posted) in the Max window.
•jvkr
1. introduction
Multiple functions can be defined in a
single script. Here, two functions have
been defined that respectively double and
triple the incoming value. The outlet()
function can be used to send data out of
js’s outlet. This function take as the first
argument the index of the outlet, after
which one or more chunks of data can be
specified, separated by commas.
Additional inlets or outlets can be created
by changing the default values of 1.
•jvkr
1. introduction
Functions called by other functions
can be defined as well. Since these
functions don’t have any meaning
outside the context in which they are
called, they can be localized in order
to prevent unwanted effects in case
they were called from outside the js
object. Attempting to call the functions
from a Max patch will then result in an
error message in the Max Console.
•jvkr
1. introduction
It is possible to work with multiple inlets in order to
create a logic we know from regular Max objects,
even though the typical strength of the js
environment lies elsewhere. In such cases, the
inlet property needs to be queried for incoming
values. The msg_int function is called when an
integer arrives at any of the inlets.
Also it is possible to provide additional arguments
when creating the js object. The first (at index 0)
is the name of the file that is loaded.
Here, a style is used where globally accessible
arguments are stored in an object.
•jvkr
1. introduction
This is what the code may look like as a
complete replacement for the + object, that
deals with both integers and floats, and
accepts a bang to retrigger the calculation.
1. introduction
This is a situation where the loadbang()
function has code that would not work at a
global level. The patch contains a float
number box which has its varname attribute
set to ‘myfloat’. The script shows a method of
setting the value of the number box from
within the script. This is one of the ways in
which Javascript and Max are tied together.
The loadbang() here is crucial; otherwise
named objects may not be found.
To be continued…