1

I've created a Javascript object with a layout like this:

var myObject : {    
     doSomething : function (args) {
         //do some stuff
         return result;
     }

     //note the args for both functions are of the same format
     doSomethingElse : function (args){
         //do some other stuff
         return otherResult
     }            
}

Now I want to achieve the following, but am not sure on how to do it in a clean way:

console.log(myObject(args));

/*
Output:
    {
         doSomething : result,
         doSomethingElse : otherResult
    }
*/

I'd like to keep both functions separate, as I would like to be able to refer to them in separate instances, but also evaluate both at once to get the desired output as above.

Thanks in advance for your help!

2 Answers 2

4

I'd imagine you'd have to iterate, something like:

var results = {},
    args = "some args";

for (var key in myObject) {
    if (typeof myObject[key] === "function") results[key] = myObject[key](args);
}

console.log(results); //should be the output you want (untested)
Sign up to request clarification or add additional context in comments.

6 Comments

I would probably at least add a check typeof myObject[key] === "function"
@MattBurland -- Ahh, yep - edited and added in the loop
@MattBurland would that prevent any non-function values being returned? I.e. if I added a third getSomethingUnrelated : "foo" would it skip over this field?
I would advise against this method. Might lead to unexpected results in certain circumstances and makes the object difficult and error-prone to modify. I would advise to use a method where you can at least list the method names you expect to be run.
@ms813: yes, but you can't do getSomethingUnrelated() anyway because it's not a function. So you'd have to add an else clause to it that would return properties
|
2

Add one more method that calls both methods (passing them the arguments) and returns your desired object output. Something like this would do:

     ...
     doBoth: function (args) {
         return {
             doSomething: this.doSomething(args),
             doSomethingElse: this.doSomethingElse(args)
         };
     }
     ...

Calling myObject.doBoth(args) will return the result you hoped for.

jsFiddle Demo


You can also do something a bit more advanced, for example listing the method names you expect to be run for the result:

     ...
     doThese: function (methods, args) {
         var result = {};
         methods.forEach(function (m) {
             result[m] = this[m](args);
         }, this);
         return result;
     }
     ...

You could invoke this with myObject.doThese(['doSomething', 'doSomethingElse'], args).

jsFiddle Demo


I would advise you not to run all the methods on the object (like other answers suggest). Seems easier at first, but will make your object difficult to modify and cause unintended behaviour in the future.

1 Comment

On closer inspection, I actually do something very similar to the first part of your answer elsewhere in my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.