简体   繁体   中英

Undefined value from Object Literal (switch replacement)

So, I'm a javascript n00b. Have heard that case/switch statements are...passé. I'm trying to wrap my head around object literals as their replacement structure.

After searching and trying various iterations in my code, I still cannot figure out why the "switch" variable value is coming back as "undefined". In my limited experience, a variable with a value of "undefined" usually means that it has no value, right? Is it a variable scope issue?

From what I gather the code is doing is creating a object (mod). The mod object has properties with the name of [3-18]. Each of these properties have values which are functions. These functions return a string value.

Here's what I've got so far:

    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"));

Here is my (non)working example: jsfiddle

Thanks in advance for your help.

The error is just you forgot the return at the end.

I think you are overengineering. This works and it's much more simple:

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: Checking a 3d6 roll?

UPDATE: Think that mod is a map, where the keys are numbers and the values are strings. When you look for a value using the key, Javascript has to compare your key with the existing ones. Check the following:

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

 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> 

Nice one; Yes, object literals are better than a switch when some of the cases returning an undefined or a specific cases only. This can be done through switch but will leave a fall-through in many cases.

I played around and came up with this logic in ES6 format. Please look into the code snippet above if that helps.

You can loop over defaultList of cases or a set of required cases. Also, If you want to parse a JSON object this is very helpful.

You have missed return on last line of your api. return modsearch;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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