18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
JavaScript Variable Naming
Is there a special purpose for using $ at the beginning of variable names?
The documentation mentions it for vars, however shows funcitons without them...
But how should one name Javascript objects? Object references? Methods? innerObjects? etc?
For example:
var $myObj = new complexObj();
normalFunction();
function normalFunction(){ // this is shown in the docs without a $ on the name
}
function complexObj(){ // this would then seem to also not need a $
this.objectVar = "": // but does this need a $ to make it this.$objectVar?
this.objectMethodRef = objectMethod; // should these? and then be called: $myObj.$objectMethodRef(); ?
this.innerObjectRef = innerObject; // how about these two
function objectMethod(){ // depending on the above, this may need a $ to match
// contents of method
}
function innerObject(){ // this might need a $ to match above too
this.innerVar = ""; // should this have a $
function innerObjMethod(){ // and for this?
// contents of inner method
}
}
}
So far when I test it, naming these both with and without $ on them seems to work.
As my code gets larger however, I would like to be doing it correctly to avoid problems.
Does it worth both ways?
The documentation mentions it for vars, however shows funcitons without them...
But how should one name Javascript objects? Object references? Methods? innerObjects? etc?
For example:
var $myObj = new complexObj();
normalFunction();
function normalFunction(){ // this is shown in the docs without a $ on the name
}
function complexObj(){ // this would then seem to also not need a $
this.objectVar = "": // but does this need a $ to make it this.$objectVar?
this.objectMethodRef = objectMethod; // should these? and then be called: $myObj.$objectMethodRef(); ?
this.innerObjectRef = innerObject; // how about these two
function objectMethod(){ // depending on the above, this may need a $ to match
// contents of method
}
function innerObject(){ // this might need a $ to match above too
this.innerVar = ""; // should this have a $
function innerObjMethod(){ // and for this?
// contents of inner method
}
}
}
So far when I test it, naming these both with and without $ on them seems to work.
As my code gets larger however, I would like to be doing it correctly to avoid problems.
Does it worth both ways?
Tagged:
Comments
Is this something that would be better directed at the Rhino Javascript interpreter code/docs?
I am working on a Sahi modification that does heuristic / adaptive selection of html page elements (to track page changes without having to recode / rerecord the test) and I need it to be as bug free as possible.
Any help would be appreciated...
Sorry for no response so far. $ variables are not a construct of javascript or required by Rhino. They are required for proper parsing of Sahi scripts.
Variables need to start with $ only if they are passed into Browser Action APIs. The reason for this is that Sahi script is first parsed before it is executed. The script itself executes in the VM of the proxy using a Rhino engine and commands are selectively sent to the browser to be executed.
For example, let us look at a simple step in Sahi:
_setValue(_textbox('username'), 'Ram');
Sahi executes it by building a string and sending it to the browser.
So this line after parsing, looks like
_sahi.schedule("_setValue(_textbox('username'), 'Ram');");
The schedule function makes the step available to the ScriptRunner as a string. The browser will eventually pick this string and execute it on the browser.
Now suppose we wish to parametrize 'Ram'. Let us do it without $
var name = 'Ram';
_setValue(_textbox('username'), name);
This will get converted to:
var name = 'Ram';
_sahi.schedule("_setValue(_textbox('username'), name);");
Notice how name is present in the string being scheduled. When this string gets evaled on the browser, name will not be resolved, or worse, may get resolved to something else on the browser.
We somehow need to substitute the value of name in the string before we schedule it. This is what $ does. If you mark a variable with a $, it will be substituted before it is sent.
So
var $name = 'Ram';
_setValue(_textbox('username'), $name);
becomes
var $name = 'Ram';
_sahi.schedule("_setValue(_textbox('username'), " + quoteIfNeeded($name) + ");");
Notice how $ variables are separated from the string so the variable values are passed rather than the variable names.
Now, regarding your function:
You do not need to use $ unless it is going to be used in an Action API. In your case, what you have written is absolutely fine.
$myObj.$objectMethodRef() is not required. It is enough to use $myObj.objectMethodRef().
As long as the parent most object ($myObj) starts with a $, Sahi will resolve it correctly.
One easy way to check if your code is fine, is to set your script for playback, and click on "View Parsed Script". It will show you what the parsed code would look like, and you will be able to learn if there are any mistakes.
Hope that helps.
Regards,
Narayan