是否可以在JavaScript(node.js)中导入变量? [英] Is it possible to import variables in JavaScript (node.js)?

查看:105
本文介绍了是否可以在JavaScript(node.js)中导入变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的变量在 app.js

I have variables in app.js:

var G = {};
module.exports = G;

var DATA = G.DATA = 'DATA';
var F1 = G.F1 = function(val)
{
  return val;
};

通过这种方式,我可以在对象 G ,同时,可以直接写入 DATA ,而不需要 G。前缀。

In this manner, I can export variables under the object G, and at the same time, can access the variable directly writing DATA without G. prefix.

到目前为止一切顺利。

现在,我想运行 test test.js app.js

var G = require('./app.js');
console.log(G.DATA);  // -> DATA

这有效,但我也想直接写入 DATA来访问变量没有 G。前缀如 console.log(DATA); // - >数据

This works, but I also want to access the variable directly writing DATA without G. prefix like console.log(DATA); // -> DATA

当然,我可以这样做

var DATA = G.DATA; 为每个变量(属性)导出&必需模块 G 对象,但显然添加每个变量是一个繁琐的过程手动对测试文件以对应 G 对象。

var DATA = G.DATA; for every variables(property) export&required module G object, but obviously it's a tedious process to add every variable to the test file manually to correspond the G objects.

有没有办法自动执行此操作?

Is there any way to do this automatically?

到目前为止,我很悲观,因为

So far, I'm pessmistic since

JS function 在自己的范围内包含 var ,所以从理论上讲,没有办法为每个对象提供一个辅助函数 var 属性。

JS function encloses var in the own scope, so in theory there's no way to have a helper function to var for every object property.

谢谢。

PS。我想避免任何 eval VM 的节点解决方案。我过去曾经尝试过,而且问题太多了。

PS. I would like to avoid any eval or VM of node solution. I have tried them in past, and too much problems.

推荐答案


我可以为每个属性导出和所需的模块G对象分配局部变量,但很明显将每个变量手动添加到测试文件以对应G对象是一个繁琐的过程。

I could assign a local variables for every property export&required module G object, but obviously it's a tedious process to add every variable to the test file manually to correspond the G objects.

不,这是它应该如何工作。您 - 只有您 - 负责模块范围中存在的局部变量。包含模块的导出变量中的任何更改都不会破坏您的代码。

No, that's how it is supposed to work. You - and only you - are in charge of what local variables exist in your module scope. No changes in the "export variables" of an included module should break your code.

访问导入模块上的属性(使用自选名称)是通往走。这相当于Python的导入应用程序导入应用程序为g

Accessing properties on the imported module (with a self-chosen name) is the way to go. This is quite equivalent to Python's import app or import app as g.

如果你想要一些特定属性作为局部变量,你通常会手动选择它们,就像在Python的中从app import DATA,F1 那样。在JS中,您需要多个 var 语句就像您在问题中显示的那样。但是,有一个名为解构分配的语法功能,这将使这更多流体。您可以在 JavaScript 1.7+(Gecko)中使用它, CoffeeScript EcmaScript 6

If you want some particular properties as local variables, you will usually choose them manually, as in Python's from app import DATA, F1. In JS, you will need a multiple var statement like the one you've shown in your question. However, there is a syntax feature called destructuring assignment which will make this more fluid. You can use this in JavaScript 1.7+ (Gecko), CoffeeScript, or EcmaScript 6:

var {DATA, F1} = require("app.js");




有没有办法自动执行此操作?

Is there any way to do this automatically?

是和否。你不应该这样做,但你可以 - 就像Python的皱眉头来自app import * 。引用他们所说的,这同样适用于JavaScript:

Yes and No. You should not do this, but you can - just like Python's frowned-upon from app import *. To cite what they say, which is equally true for JavaScript:


[它]在解释器中引入了一组未知的名称,可能是
隐藏了你已定义的一些东西。

[It] introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.

请注意,一般来说,从模块或
包导入 * 的做法不赞成,因为它经常会导致代码难以理解。
但是,可以使用它来保存交互式会话中的输入。

Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

在JavaScript中,你可以 [1] 使用 -statement

In JavaScript, you can[1] use the with-statement:

with (require("app.js")) {
    …
}

[1]:不在ES5.1 严格模式 ,但是 - 建议用于优化

这篇关于是否可以在JavaScript(node.js)中导入变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆