Quantcast
Channel: User Max - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Javascript: given list of functions, how to create an object having those functions as properties?

$
0
0

Given:

var f1 = function(){return "a";};
var f2 = function(){return "b";};
var f3 = function(){return "c";};
transformToObject([f1, f2, f3]);

How can I create a function:

function transformToObject(arrayOfFuncs){...};

which returns:

{
    f1: function(){return "a";},
    f2: function(){return "b";},
    f3: function(){return "c";}
}

The difficulty seems to get the name of the variable to create a property of the object.

Notice: since I define functions in the form of assignment of anonymous functions to variables, the "name" property of functions won't help because it's empty, which means:

function a(){};
//a.name === "a"
var b = function(){};
//b.name === ""

Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>