我目前正在使用HeadFirst JS书学习 JavaScript。直到这一刻,我都明白了一切。我将代码放在文本编辑器中以检查输出。控制台中没有任何错误;因此,代码是正确的。但我仍然无法理解要点。我认为这个问题与return
语句有关(它是如何工作的,等等),因为当我尝试这个console.log
函数时,我可以看到一些结果。
var x = 32;
var y = 44;
var radius = 5;
var centerX = 0;
var centerY = 0;
var width = 600;
var height = 400;
function setup(width, height) {
centerX = width/2;
centerY = height/2;
}
function computeDistance(x1, y1, x2, y2) {
var dx = x1 - x2;
var dy = y1 - y2;
var d2 = (dx * dx) + (dy * dy);
var d = Math.sqrt(d2);
return d;
}
function circleArea(r) {
var area = Math.PI * r * r;
return area;
}
setup(width, height);
var area = circleArea(radius);
var distance = computeDistance(x, y, centerX, centerY);
alert("Area: " + area);
alert("Distance: " + distance);