cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript Samples

ryan_perneel
Contributor

Javascript Samples

Hi, 

 

I am currently looking into converting our project from jscript to javascript and I am hitting a seemingly simple problem.  I cannot seem to call/access functions or variables defined in another script unit.  In our current implementation I am able to say ScriptUnit.functionX() from any other script unit.  I cannot seem to get this working with javascript, and seems very simple....

6 REPLIES 6
RichLC
New Contributor

Re: Javascript Samples

Hi Ryan,

You need to add 'require' statements to each of the modules (scripts) that you wish to use another module's commands such like the following:-

var GlobalValues = require("GlobalValues");

 

Access to components within GlobalValues is then possible implementing the period notation such like:

GlobalValues.[COMPONENT TO CALL]

 

In addition, the module that contains re-usable items will need to 'export' their interfaces such like:

module.exports.gTiming = true;

 

 

Hope this helps?

ryan_perneel
Contributor

Re: Javascript Samples

is there any way to add a full class to the exports.  say i have a class called Constants, and I want these to be available for use everywhere, do i need to add a module.exports.blah to everything?

 

 

RichLC
New Contributor

Re: Javascript Samples

I'm afraid so. Though the best approach here would be to configure the values as properties within an object which would mean you would only have to export the object not each value/global.

 

BTW, in addition to the export statement I believe the 'USEUNIT ' command carries across from JScript.

tristaanogre
Community Hero

Re: Javascript Samples

I prefer USEUNIT as it is the easiest to implement.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
ryan_perneel
Contributor

Re: Javascript Samples

so you can still use "//USEUNIT Blah" with Javascript?  are you still hindered by CircularReferences using this approach?

tristaanogre
Community Hero

Re: Javascript Samples

I think that, since it's a different dll, it MIGHT not be restricted in JavaScript.  You can try it but if you're not already using circular references, then it's no loss if you continue in that vein


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors