在bar.html示例文件中,它包含以下代码:
<!doctype html>
<html>
<head>
<title>Bar Chart</title>
<script src="../Chart.js"></script>
</head>
<body>
<div style="width: 50%">
<canvas id="canvas" height="450" width="600"></canvas>
</div>
<script>
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var barChartData = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,0.8)",
highlightFill : "rgba(151,187,205,0.75)",
highlightStroke : "rgba(151,187,205,1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
]
}
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
responsive : true
});
}
</script>
</body>
</html>
在代码中它有window.myBar = new Chart ....
myBar是什么?这只是一个像其他变量一样的变量吗?窗户是什么在它面前呢?
答案 0 :(得分:2)
window.myBar = new Chart(ctx).Bar(barChartData, {
responsive : true
});
<强> window.myBar
强>
这是对紧随其后创建的代码的global变量的引用。稍后,myBar
可以单独引用它。 JavaScript代码中应避免使用全局变量。
<强> = new Chart(ctx)
强>
使用constructor语法调用Chart()
函数,并传递给<canvas>
的绘图上下文。
<强> .Bar(barChartData, {
responsive : true
});
强>
调用Bar()
,Chart
的方法,传递barChartData
和文字对象{ responsive: true }
。像这样使用literal object是Bar()
用于更改其行为方式的一组选项。