代码建议 - 如何使代码更简洁(Javascript/Jquery)
我试图使我的代码更加简洁(即减少重复的代码)。我从主管那里得到了一些关于如何使我最近的代码更加简洁的建议,但我不太确定该怎么做。
我有一些坐标,用于检查用户是否在 div 的特定区域内单击。有人告诉我,我应该将所有坐标放入一个数组中,并“循环”以在需要时获取它们。我想我明白他的建议,但我无法完全理解它,因为我对编程仍然缺乏经验。以下是我到目前为止所做的事情,以便您可以更好地了解正在发生的事情:
$("#div1").click(function(e)
{
// Arrays containing the x and y values of the rectangular area around a farm
// [minX, maxX, minY, maxY]
var div1_Coord_Area1= [565, 747, 310, 423];
var div1_Coord_Area2= [755, 947, 601, 715];
if(areaX >= Reg2_Coord_Farm1[0] && areaX <= Reg2_Coord_Farm1[1] && areaY >= Reg2_Coord_Farm1[2] && areaY <= Reg2_Coord_Farm1[3])
{
alert("You clicked in the first area");
}
if(areaX >= Reg2_Coord_Farm2[0] && areaX <= Reg2_Coord_Farm2[1] && areaY >= Reg2_Coord_Farm2[2] && areaY <= Reg2_Coord_Farm2[3])
{
alert("You clicked in the second area");
}
});
不要担心我如何进行计算;我将该代码保留在方法之外,因为它基本上是无关紧要的,但它就在那里,以防您想知道。
我为每组坐标创建了一个数组并简单地调用它们。然而,这并不是“循环”一个充满每个区域的所有坐标的巨大数组。你能想出一种方法来做到这一点吗?或者这是我目前能做的最好的方法?
I'm trying to make my code more concise (i.e., less repeated code). I've gotten some advice from my supervisor as to how to make my recent code more concise, but I'm not exactly sure how to do it.
I have some coordinates that I am using to check if the user clicks within a certain area of a div. I was told that I should put all the coordinates in an array and "loop through" to get them when I need them. I -think- I understand what he was suggesting, but I can't quite wrap my head around it since I'm still inexperienced with programming. Here's what I have done so far so you can get a better idea of what's going on:
$("#div1").click(function(e)
{
// Arrays containing the x and y values of the rectangular area around a farm
// [minX, maxX, minY, maxY]
var div1_Coord_Area1= [565, 747, 310, 423];
var div1_Coord_Area2= [755, 947, 601, 715];
if(areaX >= Reg2_Coord_Farm1[0] && areaX <= Reg2_Coord_Farm1[1] && areaY >= Reg2_Coord_Farm1[2] && areaY <= Reg2_Coord_Farm1[3])
{
alert("You clicked in the first area");
}
if(areaX >= Reg2_Coord_Farm2[0] && areaX <= Reg2_Coord_Farm2[1] && areaY >= Reg2_Coord_Farm2[2] && areaY <= Reg2_Coord_Farm2[3])
{
alert("You clicked in the second area");
}
});
Do not worry about how I do the calculations; I left that code out of the method since it is basically irrelevant, but it is there in case you were wondering.
I made an array for each set of coordinates and simply call those. This isn't "looping through" a giant array filled with all of the coordinates of each area, however. Can you conceive of a way of doing this, or is the best I could do at the moment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为他的意思更像是这样的:
这样,您可以检查更多区域,但永远不必更改循环,因为它总是会迭代数组中的所有项目,无论是上面的 2 个项目,还是 10 个项目,或 100。
I think what he means is something more like this:
This way, you can have many more areas to check but never have to change the loop as it will always iterate through all items in the array, be it 2 like above, or 10, or 100.
乍一看,您可以将点击区域提取到一个单独的函数中。
像这样的东西。
您似乎在
var div1_Coord_Area1= [565, 747, 310, 423];
和var div1_Coord_Area2= [755, 947, 601, 715];< 中也有一些“神奇”数字/code> 我会考虑将它们设为全局变量,这样它们就超出了单击函数的范围。
它看起来像
希望你能看到我的流程是进一步完善的流程之一。不要期望在第一次创建方法时编写干净的代码。你必须事后看它,看看它是否讲述了一个故事。另一个更改是
div1_Coord_Area1
的名称,该名称是否真的反映了变量的意图?不。HotSpotArea1
是否意味着更多?请记住,您正在用代码讲述一个故事。你做得越多,下一个人用最少的大脑周期来阅读代码就越好。您必须不断完善才能获得真正干净的代码。
From first and a very quick glance you can extract the click area into a separate function.
Something like this.
You also seem to have some 'magic' numbers in the
var div1_Coord_Area1= [565, 747, 310, 423];
andvar div1_Coord_Area2= [755, 947, 601, 715];
I would consider making them global variables so they are out of the scope of the click function.It would read like
Hopefully you can see my process is one of further refinement. DO NOT expect to write clean code when you first create the method. You have to look at it afterward and see if it tells a story. Another change would be the name of
div1_Coord_Area1
, does the name really reval the intent of the variable? No. WouldHotSpotArea1
mean more? Remember you are telling a story with code. The more you can do so the next person uses the least number of brain cycles to read the code the better.You have to constantly refine to get really clean code.
如果我是你,我会创建你的区域对象:
现在,你可以像这样创建一个 ClickArea 对象:
但你会希望它们在一个数组中,所以我建议像这样创建它们:
然后你可以循环它们使用 for 循环:
If I were you, I'd make your areas objects:
Now, you can create a ClickArea object like this:
But you'll want them in an array, so I'd suggest creating them like this:
You can then loop over them using a for loop: