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

标签 javascript jquery variables event-handling

javascript 新手又来了。

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

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

我不明白的是为什么我在点击事件处理函数之外定义的所有函数都需要在事件处理函数内保存为新变量。这让我很困惑。为什么我不能只调用这些函数?在下面查看我的整个脚本:

<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 函数保存在事件处理程序内的新变量中。如果我试图简单地在事件处理程序中调用这些函数而不先将它们保存到新变量中,游戏将无法按预期运行。如果我在事件处理程序中定义这些函数然后调用它们,情况也是一样的。有人可以帮我理解为什么会这样吗?非常非常感谢。

最佳答案

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

对于分配给变量 getDistance 的函数引用,当您调用该函数并传入一个 event 和一个 target 时 < em>返回一个计算出来的数字。这是您分配给名为 distance 的变量以在其他地方使用

的计算数字

关于javascript - 为什么在事件处理程序函数外部定义的函数需要保存在事件处理程序内部的新变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50194629/

相关文章:

javascript - 单击文本输入时清除单选按钮

javascript - 无法将 shapefile 转换为 geojson

javascript - 与 IE7 的兼容性

javascript - 随机视频在 Chrome 中不起作用

jquery - 如何通过元素名称从追加子项中获取值?

javascript - 解析json并输出html

javascript - 我有两个包含一些重复信息的数据结构。有没有办法使用 .map 来减少这种重复?

linux - 如何在shell脚本中收集变量中的多行

r - 找出 R H2O AutoML 模型最有贡献的变量/特征?

javascript - JavaScript 中的 [] 是什么意思?