SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    501
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating a Function on the Fly

    i have a switch/case statement that i'm wanting to make more dynamic. basically, it currently looks like this...

    Code:
    function mySwitch(id) {
         switch (id) {
              case "A":
                   return "whatever";
              case "B":
                   return "whatever";
         }
    }
    i want to create this function on the fly so i can dynamically determine the different case statements. something like...

    Code:
    var func = "function mySwitch(id) { switch(id) {";
    for (var i = 0; i < [object/array].length; i++) {
         func += "case '" + [object/array].name + "':";
         func += "return '" + [object/array].value + "'";
    }
    func += "}}";
    eval(func);
    i don't get an error with the eval(), but whenever i try to call on the function it doesn't work. ideas?

  2. #2
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have the right idea. Assign the generated function string to a variable and scrutinize its validity (post it here if you are unsure).
    Code JavaScript:
    var cube = eval("function(value) { return value * value * value; }");
    cube(3); // 27
    But if all you want is a simple map of values, you'd be better of doing something simpler:
    Code JavaScript:
    var map = {};
    for (var i = 0; i < [object/array].length; i++)
    {
        map[[object/array].name] = [object/array].value;
    }
    Although, looking at that after I've written it, I don't understand what you are trying to accomplish. As far as I can see, the answer is almost nothing.

  3. #3
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    501
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i actually just figured it out. i tried a different approach...

    Code:
            var func = 'mySwitch = function(id) { switch (id) {';
            for (var i = 0; i < [object].length; i++) {
                func += 'case "' + [object].id + '":\n';
                func += 'return "' + [object].name + '";\n';
            }
            func += '}}';
            eval(func);
    this works well.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're basically looking for the associative array (aka "hash") implementation, this is bultin in javascript, no need to invent your own syntax:

    Code:
    map = {
       foo: "bar",
       quux: "blah"
    }
    alert(map.foo)
    alert(map["qu" + "ux"])


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •