Forum Discussion

cmpenn's avatar
cmpenn
Contributor
9 months ago

Calling JavaScript Classes from other files

Hi all,

I was trying to update some of our old JScript files and was hoping to use JavaScript classes for at

least a few cases...unfortunately, I can't seem to do so.

I cannot seem to get the class to be called from another file, and looking at TestComplete documentation, the only way to do that was deprecated a while ago.

Does anyone have any examples of how to do this, or am I right in that it cannot be done?

I've tried using require and using const, but no luck. Below is a sanitized version of what I wrote.

The Class:

<SPAN class="token keyword">class</SPAN> <SPAN class="token class-name">MainClass</SPAN> <SPAN class="token punctuation">{</SPAN>
    <SPAN class="token function">constructor</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token punctuation">)</SPAN> <SPAN class="token punctuation">{</SPAN>
    <SPAN class="token punctuation">}</SPAN>
    <SPAN class="token function">Start</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token punctuation">)</SPAN> <SPAN class="token punctuation">{</SPAN>
    <SPAN class="token function">Close</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token punctuation">)</SPAN> <SPAN class="token punctuation">{</SPAN>
    <SPAN class="token punctuation">}</SPAN>    
<SPAN class="token punctuation">}</SPAN>

module<SPAN class="token punctuation">.</SPAN>exports <SPAN class="token operator">=</SPAN> <SPAN class="token punctuation">{</SPAN><SPAN class="token literal-property property">MainApp</SPAN><SPAN class="token operator">:</SPAN> MainClass<SPAN class="token punctuation">}</SPAN>

The file I'm calling it from:

<SPAN class="token keyword">var</SPAN> main <SPAN class="token operator">=</SPAN> <SPAN class="token function">require</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token string">"Main"</SPAN><SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>

<SPAN class="token keyword">const</SPAN> mainApp <SPAN class="token operator">=</SPAN> <SPAN class="token keyword">new</SPAN> <SPAN class="token class-name">main<SPAN class="token punctuation">.</SPAN>MainApp</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>

mainApp<SPAN class="token punctuation">.</SPAN><SPAN class="token function">Start</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
  • That does not seem to work for the Class. It does work for functions.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Could you upload both of your codes again, as it doesn't seem to be correctly formatted.

    "That does not seem to work for the Class" - what exactly have you tried?

    • cmpenn's avatar
      cmpenn
      Contributor

      Oh yeah...something really went wrong with those.

      As for what I tried...I tried exporting the class, and I've tried requiring the class. I even tried following the module format to see if that would do anything...but I got errors that the constructor could not be called.

      The Class File

      class MainClass{
          constructor() {
          }
          Start() {
          }
          Close() {
          }    
      }

      module.exports = {MainApp: MainClass}


      The file calling the class

      var main = require("Main");

      const mainApp = new main.MainApp();

      mainApp.Start();