Is it Possible to Create a Class Inside the JS Object?

Emmett Palaima's icon

Hi, I'm trying to create a class inside the js object, but I keep get a weird error message:

js: walker.js: Javascript SyntaxError: missing ; before statement, line 4
js: source line: public class tone{

The documentation is pretty light for how js works in Max, and it doesn't mention anything about how to properly declare a class. I'm writing something pretty simple in order to test whether this works:


inlets = 1;
outlets = 1;
public class tone {
    int index;
    public tone(int index_) {
index = index_;
    }
}
function bang()
{
    post("test");
    outlet(0, 1);
}

I can't really see anything that would be causing the error, so is it just not possible to use classes with the js object? Or is there something I need to do differently with the syntax?

Chadwick Wood's icon

Hey Emmett, I don't think that the Max implementation of Javascript supports the "class" keyword. It runs Javascript 1.6.

You can do classes the "older" javascript way though, like this:

// a constructor
function MySuperclass(somearg) {
  this.someprop = somearg
}

// another constructor that inherits from the first class
function MyClass(argument) {
 MySuperclass.call(this, argument)
 this.childprop = 3
}

// set the superclass as the base prototype for the child class
MyClass.prototype = Object.create(MySuperclass.prototype)

// example of an instance method
MyClass.prototype.someMethod = function(newval) {
  this.childprop = newval
}
Jan M's icon

it is possible to use class/prototype the way Chadwick describes it. in my personal experience JS tends to become unstable in Max when classes become more complex. Especially when using dependency injection.