Tomek Buszewski
Tomek Buszewski

Reputation: 7955

Access function inside function inside object

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?

Upvotes: 0

Views: 81

Answers (2)

Mike Cheel
Mike Cheel

Reputation: 13116

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();

Upvotes: 1

David Holmes
David Holmes

Reputation: 41

 var test_func = {
        test1:function(){
           console.log("test1")
           return {
               test2:function(){
                  console.log("test2")
               }
           }
        }
    }
      test_func.test1().test2()

Upvotes: 0

Related Questions