Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-17-2011, 01:31 PM   PM User | #1
christianlett
New to the CF scene

 
Join Date: Jul 2011
Location: Cardiff
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
christianlett is an unknown quantity at this point
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:
Code:
var mySetting = new ScriptSetting("myScript", "thisSettingKey", 200);
This calls the ScriptSetting constructor function:
Code:
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;
		this.saveSetting();
	} 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.

Code:
mySetting = 300;
mySetting.saveSetting();
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,

Christian
christianlett is offline   Reply With Quote
Old 07-17-2011, 03:01 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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()
{
    return 
this.value;

__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:23 AM.


Advertisement
Log in to turn off these ads.