View Full Version : Giving an object a custom value

07-17-2011, 02:31 PM
Hi all.

I'm developing After Effects scripts using their Extendscript language - essentially just Javascript with various extensions.

I've defined a class, called ScriptSetting, which handles reading and writing of user settings to a prefs file.

For example I create a ScriptSetting variable as follows:

var mySetting = new ScriptSetting("myScript", "thisSettingKey", 200);

This calls the ScriptSetting constructor function:

function ScriptSetting(sectionName, keyName, defaultValue) {
this.sectionName = sectionName;

// Store the type of this setting for conversion later
this.defaultType = defaultValue.constructor;

// Convert any type to a string value for storing
defaultValue = defaultValue.toString();

this.keyName = keyName;
if(!app.settings.haveSetting(this.sectionName, this.keyName)) {
this.value = defaultValue;
} else
this.value = this.getSetting();

This calls some of Extendscript's built-in functions, like app.settings.haveSetting()

This all works fine - I can then later access mySetting's value with, for example alert(mySetting.value) since I've defined value as part of the class constructor function.

However what I'd ideally like to do, if at all possible is to lose the .value part and just be able to access the setting value directly from the setting variable, so in this case alert(mySetting). At the moment, this will just return object Object. Naturally this is because mySetting is an instance of the ScriptSetting object. But I wondered if you can spoof this with a different type (i.e. Array or Number) when accessing it as part of a statement e.g. if(mySetting == 200) { // do something } but still retain all its prototyped methods, e.g.

mySetting = 300;

Is this possible in Javascript? It's not a deal-breaker - I just want to make my code a bit more readable.

Many thanks in advance,


07-17-2011, 04:01 PM
I think you would need to overwrite the toString() method. it is automatically called, if there is a string representation (for output) required.

something like
ScriptSetting.prototype.toString = function()
return this.value;