简体   繁体   English

如何在 TypeScript 中为模块声明变量?

[英]How can I declare variable for a module in TypeScript?

I have two files: index.ts and A.ts我有两个文件: index.tsA.ts

A.ts : A.ts

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

index.ts : 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?someVarA.ts声明index.ts而不包装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?我知道 Node.JS 将所有模块包装在(function (exports, require, module, __filename, __dirname, process, global) { }如何更改 Node.js 模块包装器?

I tried to make a custom require function and pass my var like an argument.我尝试自定义 require function 并像参数一样传递我的 var。 But I don't understand how can I make own require function in TypScript.但我不明白如何在 TypScript 中创建自己的 require function 。 Are there any ideas?有什么想法吗?

What are you trying to do?你想做什么? The seperation for module is on purpose, so the scope of everything remains.模块的分离是故意的,所以所有东西的 scope 仍然存在。

Next, you have a typo: it should probably be let a = new A();接下来,您有一个错字:应该是let a = new A(); not let a = new A; let a = new A; . .

But why dont you just pass the variable as an argument to the constructor of your A class?但是为什么不将变量作为参数传递给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();
}

The scope of variables depends on where they are defined, not where they are called.变量的 scope 取决于它们的定义位置,而不是调用位置。 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.这是故意的,因此您不会意外调用您不知道与您的函数调用在同一个 scope 中的变量。

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:您必须明确告诉代码您想将这个新变量传递给它,就像 Lux 显示的那样,或者通过将它传递给 function ,如:

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:所以模块内部的“this”关键字是指模块的全局 scope,所以我尝试了以下代码段:

 // 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:并且似乎上下文已被删除,与使用 mjs 文件导入 ES6 相同,但是 Work 是这样的:

 //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.似乎moduleContext指向旧的模块上下文object,而新导入的模块具有不同的上下文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.不过,这似乎仍然是个坏主意,您最好构造代码,以便导出构造 function,该构造需要为 scope 设置“全局”所需的任何内容,并将其设置在内部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM