Quantcast
Viewing latest article 13
Browse Latest Browse All 40

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

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 latest article 13
Browse Latest Browse All 40

Trending Articles