Namespaces

Variants
Actions

Javascript in Max 07 - the Global Object

From Cycling '74 Wiki
Jump to: navigation, search

The Global object is a fairly generic Javascript object that allows you to share data among js instances by adding and accessing properties. You can also access Global object properties from Max messages completely outside of js. Executing methods stored in Global objects from Max is not supported. However, methods are certainly among the kinds of things you can store within a Global object.

Contents

Global Constructor

g = new Global(name);

name represents a String that uniquely identifies the Global.

A Global is basically a reference to a Javascript object that you can't access directly. The object is connected to the Max symbol with the name you supplied as an argument (this allows it to be accessed from Max). Every time you access a Global, it hands off the access to the secret hidden Javascript object. This means you can create any number of Global objects in your code, in any number of js instances, and if they all have the same name, they will all share the same data. In this way, a Global resembles a namespace.

Example:

g = new Global("name");
g.bob = 12;
h = new Global("name");
post(h.bob);  // will print 12

Global Properties

There are no fixed properties for a Global object. Instead, you assign properties to a Global object (described above) so that they can be accessed by multiple js object instances.

Global Methods

sendnamed (receive_name, property_name)

Sends the value of the named property property_name to the named Max receive object (or other Max object) bound to the specified receive_name symbol.

Example:

g = new Global("xyz");
g.ethyl = 1000;
g.sendnamed("fred","ethyl");

Any receive objects named fred will send 1000 out their outlets.

Accessing the Global Object from Max

To use Max to send a message to a named object, type a semicolon followed by the name of the receiver and the message you want to send into a message box. To set a property of a js Global object, send the property name followed by one or more values (multiple values set the value of the property to an array). Assuming you have already created a Global xyz object...

This sets the value of the george property to 30.

; xyz george 30

This sets the value of the frank property to an array of three strings containing "x" "y" and "z"

; xyz frank x y z

You can also use the message sendnamed from Max to output property values to named receive objects. This sends the current value of the frank property in the js Global object xyz to any receive objects named hubert.

; xyz sendnamed hubert frank

Note a subtle distinction. When setting property values using Max, the Javascript properties are changed but no further action happens. When using sendnamed(), receive objects take action and output the property values.