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

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

问题描述

我在 app.js 中有变量:

var G = {};module.exports = G;var DATA = G.DATA = '数据';var F1 = G.F1 = 函数(val){返回值;};

这样我就可以导出对象G下的变量,同时可以不用G,直接写DATA访问变量. 前缀.

到目前为止一切顺利.

现在,我想在 test.js 中为 app.js 运行一个测试:

var G = require('./app.js');控制台日志(G.DATA);//->数据

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

当然,我可以这样做

var DATA = G.DATA; for each variables(property) export&required module G 对象,但显然将每个变量添加到测试中是一个繁琐的过程手动文件以对应 G 对象.

有没有办法自动做到这一点?

到目前为止,我很悲观

JS functionvar 包含在自己的作用域中,所以理论上没有办法为每个对象属性都有一个辅助函数来 var.

谢谢.

附注.我想避免节点解决方案的任何 evalVM .我过去尝试过它们,但问题太多.

解决方案

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

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

访问导入模块上的属性(使用自己选择的名称)是要走的路.这相当于 Python 的 import appimport app as g.

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

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

<块引用>

有没有办法自动做到这一点?

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

<块引用>

[It] 将一组未知的名称引入解释器,可能隐藏一些你已经定义的东西.

请注意,通常从模块中导入 * 的做法或package 不受欢迎,因为它通常会导致代码可读性差.但是,可以使用它来节省交互式会话中的输入.

在 JavaScript 中,您可以[1] 使用 with 语句:

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

[1]:不在 ES5.1 严格模式,虽然 - 推荐用于优化

I have variables in app.js:

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

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

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.

So far so good.

Now, I want to run a test for app.js in test.js:

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

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

Surely, I could do like

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 encloses var in the own scope, so in theory there's no way to have a helper function to var for every object property.

Thanks.

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

解决方案

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.

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.

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?

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.

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

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

[1]: Not in ES5.1 strict mode, though - which is recommended for optimisation

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

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