1

I have two files: index.ts and A.ts

A.ts:

export default class {
    do() {
        console.log(someVar);
    }
}

index.ts:

import A from './A';

function printIt(param) {
    let someVar = param;
    let a = new A();
    a.do();
}

printIt('wow'); // Console output: wow
printIt('123'); // Console output: 123

Is it real to declare someVar for A.ts from index.ts without wrapping A class?

I know that Node.JS wrappes all modules in (function (exports, require, module, __filename, __dirname, process, global) { }: How to change the Node.js module wrapper?

I tried to make a custom require function and pass my var like an argument. But I don't understand how can I make own require function in TypScript. Are there any ideas?

2 Answers 2

2

The scope of variables depends on where they are defined, not where they are called. this is on purpose, so you do not accidentally call on variables you did not know about being in the same scope as your function's invocation.

You must explicitly tell the code you want to pass this new variable into it, either just like Lux showed, or through passing it to the function like:

export default class {
     do(someVar) {
         console.log(someVar);
     }
}
function printIt(param) {
   let someVar = param;
   let a = new A();
   a.do(someVar);
}

what you're trying to do is akin to having everything be a global variable. if you MUST do this (you shouldn't), there is one way you can.

export default class {
     do() {
         console.log(global.someVar);
     }
}
function printIt(param) {
   global.someVar = param;
   let a = new A();
   a.do();
}

There's many reasons why you do not want to do global variables, here are some

Edits after clarification:

So the "this" keyword inside of a module refers to the module's global scope, so I tried the following snippet:

// modA.js
const moduleContext = this

class ExportedClass {
    printer() {
        console.log(moduleContext.someVar)
    }
}

module.exports = { ExportedClass }


//modB.js
let A = require("./modA")

A.someVar = "hello world"

let obj = new A.ExportedClass()

obj.printer()

and it seems the context was removed, the same thing with ES6 imports using mjs files, what did Work however is this:

//modA.js
function printer() {
    console.log(this.someVar)
}

module.exports = { printer }

//modB.js
let A = require("./modA")

A.someVar = "hello world"

A.printer()

it seems moduleContext points to the old module context object, and the new imported module has a different context object.

This still seems like a bad idea though, you're better off structuring your code so that you export a constructing function, that takes whatever needs to be "global" for that scope, and sets it inside.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't want to make someVar global. I want to simulate a global variable only for one module. I know that I can pass someVar like a parameter to do or constructor. But this is not what I need.
1

What are you trying to do? The seperation for module is on purpose, so the scope of everything remains.

Next, you have a typo: it should probably be let a = new A(); not let a = new A;.

But why dont you just pass the variable as an argument to the constructor of your A class?

export default class {
    someVar: string;
    constructor(someVar) {
      this.someVar = someVar;
    }
    do() {
        console.log(this.someVar);
    }
}

now you can just do

function printIt(param) {
    let someVar = param;
    let a = new A(someVar);
    a.do();
}

5 Comments

I'm trying to simulate a global variable for A class. I know that I can save my var like a property in A class. But I'm trying to make the code shorter. I need to pass to all my modules one variable that will provide access to the application. I can't declare a variable globally, since there can be several instances of application.
new ClassOrFunction is valid. It news it without arguments.
With instances you mean full processes? If yes, why not import a function from a third module that returns the variable, and have another function to set it?
I mean const app = new App();. I need to put app var to some modules. I'm not sure that the third module if the right thing.
if you want to have a variable per class instance you really should have it per class instance. So store it on the class instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.