我想調用init()
函數中的幾個函數,其中存儲了所有共享變量。但是,這些變量不會傳遞給每個單獨的函數。任何人都可以告訴我,如果這是最乾淨的方式,或者有更好的設計?謝謝!函數中的JavaScript調用函數
function scatterPlot(data) { /* code to do scatter plot */ }
function bestFitLine(data) { /* code to draw a regression line */ }
function drawAxes(data) { /* code to draw the axes */ }
function drawLegend(data) { /* code to draw legend */ }
function init() {
data = { /* data object in JSON format */ }
margin = { /* margin settings to be passed into each function */ }
varNames = { /* some name information to be pass into each function */ }
/* More common information to be passed */
scatterPlot(data, margin, varNames, ...);
bestFitLine(data, margin, varNames, ...);
drawAxes(data, margin, varNames, ...);
drawLegend(data, margin, varNames, ...);
}
將所有內容都包含在另一個擁有共享變量的函數中? –