r3 - 13 Sep 2006 - 01:51:06 - TanDunlinYou are here: TWiki >  Main Web  >  ElixirReport > ElixirReportCookbook > JavaScriptSubstitution > JavaScript

JavaScript

Elixir tools use JavaScript as the embedded scripting language. The implementation is based on the Mozilla Rhino engine (http://www.mozilla.org/rhino/).

Here are some links to common JavaScript issues:

Addition and Concatenation, Variables with Spaces

JavaScript is a weakly typed language where every function allows any number of parameters to be passed. For example, if you have a multiply function:

function multiply(a,b)
{
return a * b;
}

then you can call this many ways.

The obvious way would be to pass in two numbers:

multiply(4,5);

returns 20.0

However, you can also pass in two strings:

multiply("4","5");

and it still returns 20.0

Of course, if you pass in

multiply("hello","5");

then the function returns NaN (not a number), but still doesn't fail.

This is all due to weak typing - Javascript doesn't define or care about the types of parameters.

Further, JavaScript doesn't care about the number of parameters.

multiply(4,5,6);

returns 20.0 because the last parameter is ignored.

multiply(4);

returns NaN because only one parameter is supplied.

None of these are considered errors.

It is possible to write multiply like this:

function multiply()
{
var result = 1;
for (i=0;i<arguments.length;i++)
result = result * arguments[i];
return result;
}

and call it with:

multiply(4); // returns 4.0
multiply(4,5); // returns 20.0
multiply(4,5,6); // returns 120.0
etc.

Thus the number of parameters is also flexible. The function doesn't even need to declare that it accepts any.

Because JavaScript supports all of these variants, any validation check can only check whether the syntax is valid, not whether it meets your specific semantic requirements. The syntax defines whether it is valid JavaScript.

Here's an excerpt from a web site, easily found with google, searching for syntax and semantics:

"A language is a set of valid sentences. What makes a sentence valid? You can break validity down into two things: syntax and semantics. The term syntax refers to grammatical structure whereas the term semantics refers to the meaning of the vocabulary symbols arranged with that structure. Grammatical (syntactically valid) does not imply sensible (semantically valid), however. For example, the grammatical sentence "cows flow supremely" is grammatically ok (subject verb adverb) in English, but makes no sense."

Typical syntax errors in JavaScript would be:

int i; // no need to declare types
var for; // variable name can't be a keyword
for (i=0) // for loop needs three clauses
multiply(4 5); // missing comma
multiply(multiply(4,5),multiply(2,3); // missing closing )
etc.

All of these are caught by the Validate check because they are all syntax errors. The JavaScript Validate function is checking syntax, it is not possible, due to the nature of the language, to automatically check JavaScript semantics.

For more details, please attend the Elixir JavaScript course.

-- JonPriddey - 19 Sep 2005

-- TanDunlin - 13 Sep 2006

Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r3 < r2 < r1 | More topic actions
 
Powered by TWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback