-1

I am just wondering if there is a way in JavaScript where I can create a new list of functions based on all "name" values in the object.

For example, with the following object:

var object_ = [{"name": "first_function", "child": ["1a", "1b"]},
               {"name": "second_function", "child": ["2a", "2b"]},
               {"name": "third_function", "child": ["3a", "3b"]}];

I would like to create the following functions:

function first_function(){
      console.log("1a, 1b");
};

function second_function(){
      console.log("2a, 2b");
};

function third_function(){
      console.log("3a, 3b");
};

Thank you

Fxs7576
  • 1,259
  • 4
  • 23
  • 31
  • Basically your array contains reference of the functions. – Shubham Nov 28 '17 at 05:40
  • 1
    What is your use case and what higher level problem are you trying to solve? – charlietfl Nov 28 '17 at 05:42
  • Are you trying to pass references to functions to `console.log()`? Or are you trying to invoke the functions and pass the output of those functions to `console.log()`? – Drew Wills Nov 28 '17 at 05:43
  • Possible duplicate of [Javascript Array of Functions](https://stackoverflow.com/questions/4908378/javascript-array-of-functions) – DragonBorn Nov 28 '17 at 05:44
  • Sorry for being unclear previously. I have edited the description. – Fxs7576 Nov 28 '17 at 05:54
  • So now with your update...what is reason for needing multiple functions that all seem to do same thing and you want to create on the fly vs one function that takes your `child` value and works with that? still not clear what higher level objective is – charlietfl Nov 28 '17 at 06:00
  • Could also be that you have misused the term *"create functions"* rather than use `name` to reference an existing function – charlietfl Nov 28 '17 at 06:03
  • @charlietfl This is only the simplified version. The actual use case has child values with more complicated and random arrays, – Fxs7576 Nov 28 '17 at 06:06
  • Fine but still not clear if you mean to match name to existing methods or what *"automatically create"* means exactly. An existing method (found by using name) is easy to pass parameters into whereas dynamically creating the business logic inside an automatically created function is a whole different story – charlietfl Nov 28 '17 at 06:08
  • @charlietfl I see. I am trying to create a non-existing method. – Fxs7576 Nov 28 '17 at 06:15
  • So now you are on the difficult path. Making a new function is one thing...telling it what to do is another. You haven't touched on that nor on what higher level problem you are trying to solve. – charlietfl Nov 28 '17 at 06:17

1 Answers1

2

If, given a list of metadata to create functions with:

var functionData = [
    { name: 'first_function', children: ['1a', '1b'] },
    { name: 'second_function', children: ['2a', '2b'] },
];

We can dynamically create a function by the name of each one's name property:

functionData.forEach(({name, children}) => {
    // create a function and put it under that name
    this[name] = () => {
        // and make sure it logs the corresponding data
        console.log(...children);
    }
})

Which, for example can then be called like so:

first_function();
// logs "1a", "1b"

Or even:

this[functionData[0].name]();
// logs "1a", "1b"

I added the functions to the this object just as an example.

This works for a non-strict-mode browser console; because an unqualified reference to a function tries to look for it on the window, which is the this context in that environment.

Bryce
  • 6,440
  • 8
  • 40
  • 64