This project has moved and is read-only. For the latest updates, please go here.

Behaviour of "var" of same variable across Execute calls

Jun 5, 2015 at 3:33 AM
Edited Jun 5, 2015 at 3:38 AM
I am attempting to implement something like the "module pattern", where different scripts can contribute to building up a single object, as described in the section entitled Loose Augmentation, http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html). The basic idea is to be able to do something like this
var MODULE = (function (my) {
    // add capabilities...

    return my;
}(MODULE || {}));
multiple times for the same MODULE in different script files.

However, it isn't working out with Jurassic, because of the way it handles "var" of the same variable across calls to Execute. Here's a simple repro of the issue:
    var engine = new ScriptEngine();
    engine.Execute("var x = 1; var x;");
    Console.WriteLine(engine.Evaluate("x")); // prints 1
    engine.Execute("var x;");
    Console.WriteLine(engine.Evaluate("x")); // prints undefined
"var x" appears 3 times: the first one sets it to 1, the 2nd has no effect. I would expect the 3rd to also have no effect, but it changes x to undefined. If I wanted to alter this behaviour, would it be difficult to do?
Jun 7, 2015 at 11:17 PM
"var x" appears 3 times: the first one sets it to 1, the 2nd has no effect. I would expect the 3rd to also have no effect, but it changes x to undefined.
That does sound wrong. I'll look into it.
If I wanted to alter this behaviour, would it be difficult to do?
The behaviour of var ties into the javascript scoping rules, which are pretty complicated. But seeing as I don't know exactly what is causing this, I can't say how difficult it would be to fix.
Jun 9, 2015 at 1:40 AM
Thanks for the reply Paul! For my situation, I've decided ultimately to take a different approach that doesn't require the module pattern, so ... not desperate for a fix anymore :)