Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Thanked 0 Times in 0 Posts

    Giving an object a custom value

    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,


  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 568 Times in 561 Posts
    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
    PHP Code:
    ScriptSetting.prototype.toString = function()

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts