Max incorrectly (?) throws syntax error when using Object.defineProperty() with accessor descriptor
The Max implemenation of JS seems to support the Object.defineProperty() method. It correctly reads in and follows keys like value and writable when the data descriptor format is used.
However, the other format used by Object.defineProperty(), the accessor descriptor, is either not supported, or is being rejected superficially due to a syntax error. From the Mozilla reference page, "The accessor descriptor is a property described by a getter-setter pair of functions", which is what I want to use, to define a custom setter/getter pair for a property.
Here is a bit of sample code:
var myObject = {};
Object.defineProperty(myObject, 'a', {
get () {
return a;
},
set (value) {
a = value;
post ("setting a to " + value);
},
enumerable: true,
configurable: true
});
When the script is loaded, Max throws this error:
js: master_control.js: Javascript SyntaxError: missing : after property id, line 4
Now, I suspect that this error is being thrown simply because Max is looking for a colon, expecting the standard format key : value. Since Max plays nice with the data descriptor format, it seems to support the defineProperty method. But it won't load the script above, due to a supposed syntax error.
Can anyone else confirm or deny this? Running Max 7.3.4 here, on OS X.
reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
Update: Reported this as a bug/issue to the devs, we'll see what they think.
Found a sort-of workaround here:
http://speakingjs.com/es5/ch17.html#getters_setters
Which is to define custom setters and getters with an object literal:
var myObject = {
foo_value : "barky",
get foo() {
post("hey i'm going to return foo now...\n");
return (this.foo_value + " dog");
},
set foo(value) {
this.foo_value = value;
}
};
post (myObject.foo + "\n");
myObject.foo = "sleepy";
post (myObject.foo + "\n");
The disadvantages to this approach are:
1. The behavior of the virtual "foo" property can't be modified later after it is first declared (unlike using Object.defineProperty(), which allows creation OR modification of a property).
2. Requires an extra property "foo_value" if you want to actually use the property "foo" as a storage space for values.
At least, that's my understanding, as a JS novice.
I did hear back from Cycling '74 support, and I'm happy to say the solution is to simply use an alternate syntax for the accessor descriptor. This solution now works for me:Object.defineProperty(myObject, 'a', {
get: function() {
return a;
},
set: function(value) {
a = value;
post ("setting a to " + value);
},
enumerable: true,
configurable: true
}
);