简体   繁体   English

Javascript switch(true) 执行错误语句?

[英]Javascript switch(true) executes false statements?

Im looping over a collection of coordinate values and doing math on the coordinates to see if the calculated values are in a hashmap.我循环遍历一组坐标值并对坐标进行数学运算,以查看计算的值是否在 hashmap 中。 if they are in the hash map then I want to run an additional function.如果它们在 hash map 中,那么我想再运行一个 function。 since I had multiple cases I wanted to check for each coord in the collection, I figured a switch statement would be cool to use to replace my if statements so all my checks could be visually and logically grouped.因为我有多个案例,我想检查集合中的每个坐标,所以我认为使用 switch 语句来替换我的 if 语句会很酷,这样我的所有检查都可以在视觉上和逻辑上进行分组。 When I replaced my if statements with a switch, my code returned bad results.当我用 switch 替换 if 语句时,我的代码返回了错误的结果。 When I debugged, I realized the switch statements would sometimes execute even when the case was false(I added console.logs to output the result of the same switch condition and it would print false, but should only run when true).当我调试时,我意识到即使情况为假,switch 语句有时也会执行(我将 console.logs 添加到 output 相同的开关条件的结果,它会打印错误,但应该只在为真时运行)。 Here is a small example:这是一个小例子:

var idm = {0:1, 3:1, 9:1, 10:1, 11:1, 12:1, 20:1, 21:1, 23:1}

var findNeighbors = function(b) {

    var u,d,l,r,lRow,rRow;
    var currentBuilding = parseInt(b);
    var currRow = Math.floor(currentBuilding/column);

    //remove value from map so we dont recount it.
    delete idm[currentBuilding];

    u = currentBuilding - column;
    d = currentBuilding + column;
    l = currentBuilding - 1;
    lRow = Math.floor(l/column);
    r = currentBuilding + 1;
    rRow = Math.floor(r/column);

    console.log("current idx:" + currentBuilding);
    console.log("u:" + u + ", d:" + d + ", l:" + l + " r:" + r);
    // debugger;
    switch(true) {
      case (idm.hasOwnProperty(u) === true):
        console.log((idm.hasOwnProperty(u)));
        console.log("map has " + currentBuilding + " -> u: " + u);
        findNeighbors(u);
      case (idm.hasOwnProperty(d) === true):
        console.log((idm.hasOwnProperty(d)));
        console.log("map has " + currentBuilding + " -> d: " + d);
        findNeighbors(d);
      case (lRow === currRow && idm.hasOwnProperty(l) === true):
        console.log((lRow === currRow && idm.hasOwnProperty(l)));
        console.log("map has " + currentBuilding + " -> l: " + l);
        findNeighbors(l);
      case (rRow === currRow && idm.hasOwnProperty(r) === true):
        console.log((rRow === currRow && idm.hasOwnProperty(r)))
        console.log("map has " + currentBuilding + " -> r: " + u);
        findNeighbors(r);
    }
    console.log("---------------------------");
  }

I figured a switch statement would be cool to use to replace my if statements so all my checks could be visually and logically grouped.我认为使用 switch 语句来替换我的 if 语句会很酷,这样我的所有检查都可以在视觉和逻辑上进行分组。

Well, write code that works not code that looks cool.好吧,编写有效的代码而不是看起来很酷的代码。 You were forgetting break statements, so the execution flow fell through - without evaluating the other case expressions after the first one matched.您忘记了break语句,因此执行流程失败了- 在第一个匹配后没有评估其他case表达式。 Btw switch ing on a constant is a horrible (uncool) practice.顺便说一句, switch常量是一种可怕(不酷)的做法。

Use standard if / else instead.使用标准if / else代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM