0
我有以下代碼:如何使一個變量全球對需要
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));
是將數據作爲我做正確嗎?是否有任何其他方式使這些函數的變量爲全局變量?
我應該如何從其他文件訪問它? – Coder
你可以編寫「global.data」或只是「數據」它的任何地方訪問。例如,如果你定義var someData = 0;在文件中,那麼它可以通過整個文件中的someData訪問。就像那樣,您只需寫入數據即可在任何文件的任何位置訪問它。 –
@PHPLover如果有幫助,請更新 –