代码建议 - 如何使代码更简洁(Javascript/Jquery)

发布于 2024-10-27 15:22:30 字数 1166 浏览 1 评论 0原文

我试图使我的代码更加简洁(即减少重复的代码)。我从主管那里得到了一些关于如何使我最近的代码更加简洁的建议,但我不太确定该怎么做。

我有一些坐标,用于检查用户是否在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里的微风 2024-11-03 15:22:30

我认为他的意思更像是这样的:

$("#div1").click(function(e){
    // Arrays containing the x and y values of the rectangular area around a farm
    // For each array: [area1, area2, area3, ... areaX]
    var minX = [565, 755];
    var maxX = [747, 947];
    var minY = [310, 601];
    var maxY = [423, 715];

    for (var i = 0; i < minX.length; i++) {
      if (areaX >= minX[i] && areaX <= maxX[i] && areaY >= minY[i] && areaY <= maxY[i]) {
        alert("You clicked in area " + (i+1));
      }
    }
});

这样,您可以检查更多区域,但永远不必更改循环,因为它总是会迭代数组中的所有项目,无论是上面的 2 个项目,还是 10 个项目,或 100。

I think what he means is something more like this:

$("#div1").click(function(e){
    // Arrays containing the x and y values of the rectangular area around a farm
    // For each array: [area1, area2, area3, ... areaX]
    var minX = [565, 755];
    var maxX = [747, 947];
    var minY = [310, 601];
    var maxY = [423, 715];

    for (var i = 0; i < minX.length; i++) {
      if (areaX >= minX[i] && areaX <= maxX[i] && areaY >= minY[i] && areaY <= maxY[i]) {
        alert("You clicked in area " + (i+1));
      }
    }
});

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.

悲歌长辞 2024-11-03 15:22:30

乍一看,您可以将点击区域提取到一个单独的函数中。

像这样的东西。

$("#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(inArea(div1_Coord_Area1, someStructForMouseLocation)) 
    {
        alert("You clicked in the first area");
    }
    if(inArea(div1_Coord_Area2, someStructForMouseLocation)) 
    {
        alert("You clicked in the second area");
    } 
});

function inArea(coordArea, mouseLocation)
{
    return mouseLocation.X >= coordArea[0] && mouseLocation.X <= coordArea[1] && mouseLocation.Y >= coordArea[2] && mouseLocation.Y <= coordArea[3]
}

您似乎在 var div1_Coord_Area1= [565, 747, 310, 423];var div1_Coord_Area2= [755, 947, 601, 715];< 中也有一些“神奇”数字/code> 我会考虑将它们设为全局变量,这样它们就超出了单击函数的范围。

它看起来像

// 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];

$("#div1").click(function(e)
{
    if(inArea(div1_Coord_Area1, someStructForMouseLocation)) 
    {
        alert("You clicked in the first area");
    }
    if(inArea(div1_Coord_Area2, someStructForMouseLocation)) 
    {
        alert("You clicked in the second area");
    } 
});

function inArea(coordArea, mouseLocation)
{
    return mouseLocation.X >= coordArea[0] && mouseLocation.X <= coordArea[1] && mouseLocation.Y >= coordArea[2] && mouseLocation.Y <= coordArea[3]
}

希望你能看到我的流程是进一步完善的流程之一。不要期望在第一次创建方法时编写干净的代码。你必须事后看它,看看它是否讲述了一个故事。另一个更改是 div1_Coord_Area1 的名称,该名称是否真的反映了变量的意图?不。HotSpotArea1 是否意味着更多?请记住,您正在用代码讲述一个故事。你做得越多,下一个人用最少的大脑周期来阅读代码就越好。

您必须不断完善才能获得真正干净的代码。

From first and a very quick glance you can extract the click area into a separate function.

Something like this.

$("#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(inArea(div1_Coord_Area1, someStructForMouseLocation)) 
    {
        alert("You clicked in the first area");
    }
    if(inArea(div1_Coord_Area2, someStructForMouseLocation)) 
    {
        alert("You clicked in the second area");
    } 
});

function inArea(coordArea, mouseLocation)
{
    return mouseLocation.X >= coordArea[0] && mouseLocation.X <= coordArea[1] && mouseLocation.Y >= coordArea[2] && mouseLocation.Y <= coordArea[3]
}

You also seem to have some 'magic' numbers in the var div1_Coord_Area1= [565, 747, 310, 423]; and var 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

// 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];

$("#div1").click(function(e)
{
    if(inArea(div1_Coord_Area1, someStructForMouseLocation)) 
    {
        alert("You clicked in the first area");
    }
    if(inArea(div1_Coord_Area2, someStructForMouseLocation)) 
    {
        alert("You clicked in the second area");
    } 
});

function inArea(coordArea, mouseLocation)
{
    return mouseLocation.X >= coordArea[0] && mouseLocation.X <= coordArea[1] && mouseLocation.Y >= coordArea[2] && mouseLocation.Y <= coordArea[3]
}

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. Would HotSpotArea1 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.

月下伊人醉 2024-11-03 15:22:30

如果我是你,我会创建你的区域对象:

// think of this as your ClickArea class
function ClickArea(minX, maxX, minY, maxY, description) {
    this.minX = minX;
    this.maxX = maxX;
    this.minY = minY;
    this.maxY = maxY;
    this.description = description;
};
ClickArea.prototype.isInside = function(areaX, areaY) {
    return (areaX >= this.minX && areaX <= this.maxX && areaY >= this.minY && areaY <= this.maxY);
};

现在,你可以像这样创建一个 ClickArea 对象:

var area = new ClickArea(565, 747, 310, 423, "Area 1");

但你会希望它们在一个数组中,所以我建议像这样创建它们:

var areas = [
    new ClickArea(565, 747, 310, 423, "Area 1"),
    new ClickArea(365, 745, 330, 423, "Area 2")
];
// you can also add new ClickAreas using the array's push method:
areas.push(new ClickArea(333, 444, 555, 566, "Area 3"));

然后你可以循环它们使用 for 循环:

for(var i = 0; i < areas.length; i++) {
    if(areas[i].isInside(areaX, areaY)) {
        alert("You clicked in " + areas[i].description);
    }
}

If I were you, I'd make your areas objects:

// think of this as your ClickArea class
function ClickArea(minX, maxX, minY, maxY, description) {
    this.minX = minX;
    this.maxX = maxX;
    this.minY = minY;
    this.maxY = maxY;
    this.description = description;
};
ClickArea.prototype.isInside = function(areaX, areaY) {
    return (areaX >= this.minX && areaX <= this.maxX && areaY >= this.minY && areaY <= this.maxY);
};

Now, you can create a ClickArea object like this:

var area = new ClickArea(565, 747, 310, 423, "Area 1");

But you'll want them in an array, so I'd suggest creating them like this:

var areas = [
    new ClickArea(565, 747, 310, 423, "Area 1"),
    new ClickArea(365, 745, 330, 423, "Area 2")
];
// you can also add new ClickAreas using the array's push method:
areas.push(new ClickArea(333, 444, 555, 566, "Area 3"));

You can then loop over them using a for loop:

for(var i = 0; i < areas.length; i++) {
    if(areas[i].isInside(areaX, areaY)) {
        alert("You clicked in " + areas[i].description);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文