0

I have the code below, as you can see I'm manually specifying the properties I want to return, I don't care about having private/public functions here, so I'd like a quick & dynamic way to just return all functions in the scope. Is this possible? I've tried a few things but having trouble, it has to be done inside the scope otherwise it can't see the functions.

var MyObj = {
        (function() {
            function MyFuncOne() {}
            function MyFuncTwo() {}
            return {
                MyFuncOne: MyFuncOne,
                MyFuncTwo: MyFuncTwo
            }
        })()
    }
    var MyObjTwo = {
        (function() {
            function MyFuncOne() {}
            function MyFuncTwo() {}
            return {
                MyFuncOne: MyFuncOne,
                MyFuncTwo: MyFuncTwo
            }
        })()
    }

Pseudo code of what I want:

function ReturnAllProperties() { //foreach func in scope append to list }

then in the return { ReturnAllProperties(); }

williamsandonz
  • 15,864
  • 23
  • 100
  • 186
  • 1
    no. potential duplicate of http://stackoverflow.com/questions/2051678/getting-all-variables-in-scope – max Mar 08 '12 at 01:46

1 Answers1

0

Reading between the lines I think you want a singleton namespace pattern.

var MYNAMESPACE = (function(){
    //Private vars
    var x, y, z;
    var MyFuncOne = function() {.....}
    var MyFuncTwo = function() {.....}
    return {
        //Here expose functions as privileged methods
        //You can also expose non-function vars (Numbers, Strings, plain Objects, Arrays) but in general you should expose only methods.
        MyFuncOne: MyFuncOne,
        MyFuncTwo: MyFuncTwo
    };
})();

By convention, namespace members are CAPITALISED.

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44