为什么在事件处理函数之外定义的函数需要保存在事件处理程序内的新变量中?

时间:2018-05-05 22:39:07

标签: javascript jquery variables event-handling

再次在这里javascript新手。

我不需要知道如何做某事,我只需要帮助理解

我刚刚在正在阅读的初学者书中完成了编码挑战,要求您制作一个用户必须在藏宝图上搜索“埋藏的宝藏”的游戏。游戏计算每次点击距离随机选择的“宝藏”位置的距离,并显示不同的更热/更冷的消息,直到玩家获胜。

我不明白为什么我在click事件处理函数之外定义的所有函数都需要在事件处理函数中保存为新变量。这让我很困惑。为什么我不能只调用那些功能?请参阅下面的整个脚本:

<script src="https://code.jquery.com/jquery-2.1.0.js"></script>

<script type="text/javascript">

//This function returns a random number that will be used as an argument to determine random coordinates for the location of the buried treasure.
var randomNumber = function (size) {
  return Math.floor(Math.random()*size);
}

//Variables used as arguments for the randomNumber function.
var width = 400;
var height = 400;

//Click counter variable.
var clicks = 0;

//Object representing the location of the buried treasure on the map.
var target = {
  x: randomNumber(width),
  y: randomNumber(height)
}

/* This function takes the event object and the buried treasure location object
and uses the Pythagorean theorem to determine a straight line between the click event
and the treasure location. Locations on the page and events on the page are always objects,
never simple variables containing a string or integer.
*/
var getDistance = function (event, target) {
  var diffX = event.offsetX - target.x;
  var diffY = event.offsetY - target.y;
  return Math.sqrt((diffX*diffX) + (diffY*diffY));
}

//This function takes distance as an argument and returns a message to the user depending on how far from the treasure location their click is.
var hints = function (distance) {
  if (distance < 10) {
    return "Red hot!";
  } else if (distance < 20) {
    return "Very hot!";
  } else if (distance < 40) {
    return "Hot";
  } else if (distance < 80) {
    return "Warm";
  } else if (distance < 160) {
    return "Cold";
  } else if (distance < 300) {
    return "Very cold!";
  } else {
    return "Ice cold! Brrrr";
  }
}

//Click handler function.
$("#map").click(function () {

  clicks++;

/* If you simply call the functions defined outside of the click handler function, the program will not
run correctly. Instead, the functions defined outside of the click handler function must be saved
in new variables and those variables used in the place of the functions in order for the program to
execute properly. This is what confuses me.
*/
  var distance = getDistance(event, target);
  var distanceHint = hints(distance);

  $("#hints").text(distanceHint);

  if (distance < 9) {
    alert("Treasure found in " + clicks + " clicks!");
  }

});




</script>

如您所见,getDistance函数和hints函数保存在事件处理程序内的新变量中。如果我试图在事件处理程序中简单地调用这些函数而不首先将它们保存到新变量,那么游戏将无法按预期工作。如果我在事件处理程序中定义了这些函数然后调用它们,则相同。有人能帮助我理解为什么会这样吗?很多,非常感谢。

2 个答案:

答案 0 :(得分:1)

您没有将该函数分配给事件处理程序中的变量....您正在分配该函数的调用 返回

如果您在调用该函数并传入getDistanceevent时将函数引用分配给变量target,则返回数。这是您分配给名为distance的变量以在其他地方使用

的计算数字

答案 1 :(得分:0)

变量distancedistanceHint已保存或更具体,在事件处理程序内分配,而不是getDistancehints

在下文中,distancedistanceHint是此处的新变量,以下仅表示其值已分配给getDistancehints的值,每当更新getDistancehints的值时,它们都会将值分别传递给distancedistanceHint变量。

var distance = getDistance(event, target); // if getDistance(event, target) = ABC, then distance is now also equal to ABC
var distanceHint = hints(distance);// if hints(distance) = XYZ, then distanceHint is now also equal to XYZ