是否可以将可选参数传递给javascript中的函数?
我在 JavaScript 中有一个函数,并且我想要 reicly,所以,我在函数内部使用 var 选项,我想将变量传递给 param。
我的功能如下:
javascript:
function DrawChartPie(rows, title, chart_div, legend_position) {
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Tipo');
data.addColumn('number', 'Quantidade');
data.addRows(rows);
var options = {
width: 450,
height: 250,
title: title,
legend: legend_position,
backgroundColor: 'transparent',
is3D: true,
chartArea: {width:"90%"}
};
var chart = new google.visualization.PieChart(document.getElementById(chart_div));
chart.draw(data, options);
}
}
帮手:
html = pie_chart(data, title, div, pos_leg)
我想做的,但它没有工作:
帮手:
def pie_data(data_pie, div, options = {})
....
optional = {
width: 900,
height: 500,
title: title,
legend: pos_leg,
backgroundColor: 'transparent',
is3D: true,
chartArea: {width:"90%"}
};
...
html = pie_chart(data, title, div, pos_leg, optional)
javascript:
function DrawChartPie(rows, title, chart_div, legend_position, optional = {}) {
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Tipo');
data.addColumn('number', 'Quantidade');
data.addRows(rows);
var option = optional || {
width: 450,
height: 250,
title: title,
legend: legend_position,
backgroundColor: 'transparent',
is3D: true,
chartArea: {width:"90%"}
};
var chart = new google.visualization.PieChart(document.getElementById(chart_div));
chart.draw(data, option);
}
}
当我尝试时,不要显示任何内容。
有可能做这样的事情吗?如果是的话,有人可以帮助我吗?
谢谢=]对不起我的英语:P
---- 更新 ---- 我更新了我的方法,如 sugest,但现在我遇到了另一个错误。
javascript:
function DrawChartPie(rows, title, chart_div, legend_position, optional) {
...
var optional = optional || {
width: 450,
height: 250,
title: title,
legend: legend_position,
backgroundColor: 'transparent',
is3D: true,
chartArea: {width:"90%"}
};
...
}
帮手:
def pie_data(data_pie, div, options = {}){
...
optional = {
width: 900,
height: 500,
title: title,
legend: pos_leg,
backgroundColor: 'transparent',
is3D: true,
chartArea: {width:"90%"}
};
html = pie_chart(data, title, div, pos_leg, optional)
}
我的错误:参数数量错误(5 比 4)
我已经重新启动了我的服务器:P