我有以下代码:
var Nightmare = require('nightmare');
var tester = require('./../_common/tester');
var testCases = require('./test-cases');
var config = require('./config.js');
var data = {
nightmare: Nightmare(config.nightmare),
config: config,
testCases: testCases,
tester: tester
};
该数据主要包含配置变量和函数。
然后我有以下一组需要所有这些配置和功能的功能:
var resultsPromise = pipeline([
commonFunctions.accessURL(data),
commonFunctions.loginToWebsite1(data),
commonFunctions.loginToWebsite2(data),
commonFunctions.loginToWebsite3(data),
commonFunctions.accessURL2(data),
])
.then(() => commonFunctions.success(nightmare))
.catch((error) => console.log(error));
我传递的数据正确吗?还有其他方法可以使这些函数的变量成为全局变量吗?
最佳答案
这很好,但如果您想在许多文件中使用此配置,那么您可以使用 global 关键字将任何变量或对象设为全局
global.data = {}
并且数据对象可以在任何地方访问
关于javascript - 如何使变量成为全局变量以满足需求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42177508/