var f1 = function(){return "a";};
var f2 = function(){return "b";};
var f3 = function(){return "c";};
var obj = {f1: f1, f2: f2, f3: f3};
alert(obj.f1());
演示
根据您的编辑,可能是这样的:
var f1 = function(){return "a";};
var f2 = function(){return "b";};
var f3 = function(){return "c";};
function transformToObject(){
return {f1: f1, f2: f2, f3: f3};
}
var myObjectHasFunctions = transformToObject();
alert(myObjectHasFunctions.f1());
演示2
或者一些非常疯狂的东西:
var f1 = function a(){return "a";};
var f2 = function b(){return "b";};
var f3 = function c(){return "c";};
var arrayOfFuncs = [f1, f2, f3]
function transformToObject(arrayOfFuncs){
var obj = {}
obj.functions = []
arrayOfFuncs.forEach(function(test){
obj.functions.push({name: test.name, method: test})
});
return obj;
}
var myObjectHasFunctions = transformToObject(arrayOfFuncs);
myObjectHasFunctions.functions.forEach(function(func){
alert(func.method());
});
演示3
但是你真的最好按照各种评论中提到的那样做(2)。