Object Literal中未定义的值(交换机替换)

时间:2014-03-04 08:21:41

标签: javascript

所以,我是一个javascript n00b。听说过case / switch语句是......passé。我试图将对象文字作为替代结构。

在我的代码中搜索并尝试各种迭代之后,我仍然无法弄清楚为什么“switch”变量值会以“undefined”的形式返回。在我有限的经验中,值为“未定义”的变量通常意味着它没有价值,对吧?这是一个可变范围问题吗?

我收集的代码正在创建一个对象(mod)。 mod对象具有名称为[3-18]的属性。这些属性中的每一个都具有作为函数的值。这些函数返回一个字符串值。

这是我到目前为止所得到的:

    function getModValue(str) {
    var search = str;
    var mod = {
        3: function() {return "-3";},
        4: function() {return "-2";},
        5: function() {return "-2";},
        6: function() {return "-1";},
        7: function() {return "-1";},
        8: function() {return "-1";},
        9: function() {return "0";},
        10: function() {return "0";},
        11: function() {return "0";},
        12: function() {return "0";},
        13: function() {return "+1";},
        14: function() {return "+1";},
        15: function() {return "+1";},
        16: function() {return "+2";},
        17: function() {return "+2";},
        18: function() {return "+3";}
    }
    mod[search]();
    }

    alert(getModValue("14"));

这是我的(非)工作示例:jsfiddle

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

错误就是你忘记了最后的return

我认为你是过度工程学的。这有效,而且更简单:

function getModValue(str) {
    var mod = {
        3:  "-3",
        5:  "-2",
        4:  "-2",
        6:  "-1",
        7:  "-1",
        8:  "-1",
        9:  "0",
        10: "0",
        11: "0",
        12: "0",
        13: "+1",
        14: "+1",
        15: "+1",
        16: "+2",
        17: "+2",
        18: "+3"
    }
    return mod[str];
    }

alert(getModValue("14"));

PS:检查3d6卷?

更新:认为mod是一张地图,其中键是数字,值是字符串。当您使用密钥查找值时,Javascript必须将您的密钥与现有密钥进行比较。请检查以下内容:

var number="1";
number==1 //true, because it's like if '==' makes a "toString()"
number===1 //false

var myObj={hello: function(){ return "Hello";}};
myObj.hello();
myObj["hello"](); // equivalent

答案 1 :(得分:1)



const responseToParse = {
  3: '-3',
  4: '-2',
  5: '-2',
  6: '-1',
  7: '-1',
  8: '-1',
  9: '0',
  10: '0',
  11: '0',
  12: '0',
  13: '+1',
  14: '+1',
  15: '+1',
  16: '+2',
  17: '+2',
  18: '+3',
  19: undefined,
  20: null
}

const MyObjectLiteralLibrary = {
  3: response => response[3],
  4: response => response[4],
  5: response => response[5],
  6: response => response[6],
  7: response => response[7],
  8: response => response[8],
  9: response => response[9],
  10: response => response[10],
  11: response => response[11],
  12: response => response[12],
  13: response => response[13],
  14: response => response[14],
  15: response => response[15],
  16: response => response[16],
  17: response => response[17],
  18: response => response[18],
  19: response => response[19],
  20: response => response[20]
}
let str
str = 14
console.log(MyObjectLiteralLibrary[str](responseToParse))

str = 19
console.log(MyObjectLiteralLibrary[str](responseToParse))

str = 20
console.log(MyObjectLiteralLibrary[str](responseToParse))

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
&#13;
&#13;
&#13;

好的;是的,当某些情况仅返回未定义或特定情况时,对象文字比切换更好。这可以通过开关来完成,但在许多情况下会留下一个漏洞。

我玩了一遍,并以ES6格式提出了这个逻辑。如果有帮助,请查看上面的代码段。

您可以遍历案例的defaultList或一组必需的案例。此外,如果要解析JSON对象,这非常有用。

答案 2 :(得分:0)

你错过了api最后一行的回报。  返回 modsearch;

相关问题