0

I know that sounds weird, but bear with me.

I have an object like this:

Functions = {
    function1: function() {
        function2: function() {
            alert('bam');
        }
    }
}

How can I fire function2? I tried Functions.function1.function2and Functions.function1().function2() but none works. Am I doing something wrong?

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
Tomek Buszewski
  • 7,659
  • 14
  • 67
  • 112

2 Answers2

1

Here are two reworkings:

Keeping the function2 label

Functions = {
    function1: function() {
        function2: 
          (function() {
            alert('bam');
          })();      
    }
};

Functions.function1(); // You still can't access the label function2 however

Removing the function2 label (switch for a return

Functions = {
    function1: function() {
        return function() {
            alert('bam');
        };
    }
};

Functions.function1()();

Bottom line is that the code doesn't work as it stand because you cannot treat a label as if it was a property of a function.

The closest you could get (as far as I can tell) to calling function2 off of function1 is (without a return statement):

Functions = {
    function1: function() {
    }
};

Functions.function1.function2 = function() {
  alert("bam");
};

Functions.function1.function2();
Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
0
 var test_func = {
        test1:function(){
           console.log("test1")
           return {
               test2:function(){
                  console.log("test2")
               }
           }
        }
    }
      test_func.test1().test2()