0

i constructed a nested object below:

var outerMost = {

    inner1: {

    innerCall: alert( "i'm the call from inner1" ),

        inner2: {

        innerCall: alert( "i'm the call from inner2" ),

            inner3: {

            innerCall: alert( "i'm the call from inner3" ),

                inner4: {

                innerCall: alert( "i'm the call from inner4" ),

                    inner5: {

                    innerCall: alert( "i'm the call from inner5" ),

                    }
                }
            }
        }
    }
};

All i want is to hear the call form inner4 by using this:

outerMost.inner1.inner2.inner3.inner4.innerCall();

But i got calls form every inner alert and was threw an error message pointing to the last expression from the debugger:

Property 'innerCall' of object #<Object> is not a function

What's wrong with my outerMost?

3 Answers 3

1

Note: I'm not going to keep repeating your entire object literal, I'll just use an abbreviated form...

Each time you said this:

{ innerCall: alert( "i'm the call from inner1" ) }

You weren't creating a property innerCall that was the alert() function, you were creating a property innerCall that was assigned the result of actually calling the alert() function at that moment. The return from alert() is undefined.

So nesting that structure five levels deep called alert() five times all in the process of creating your object, but the actual innerCall properties were all set to undefined. So then this:

outerMost.inner1.inner2.inner3.inner4.innerCall();

Was like saying undefined(); - which of course doesn't work.

What you need is for innerCall to reference a function which in turn calls alert():

{ innerCall : function() { alert("I'm the call from inner1"); } }

That is more or less equivalent to doing this:

function myInnerCallFunction1() {
   alert("I'm the call from inner1");
}

{ innerCall : myInnerCallFunction1 }

(Note that in the object literal myInnerCallFunction1 does not have parentheses so it is a reference to the function rather than executing the function.)

Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to specify that your innerCalls objects are functions... Correct code below

var outerMost = {

    inner1: {

    innerCall: function() {alert( "i'm the call from inner1" )},

        inner2: {

        innerCall: function() {alert( "i'm the call from inner2" )},

            inner3: {

            innerCall: function() {alert( "i'm the call from inner3" )},

                inner4: {

                innerCall: function() {alert( "i'm the call from inner4" )},

                    inner5: {

                    innerCall: function() {alert( "i'm the call from inner5" )},

                    }
                }
            }
        }
    }
};

Comments

0

You have to do it this way:

var outerMost = {

inner1: {

    innerCall: function () {
        alert("i'm the call from inner1")
    },

    inner2: {

        innerCall: function () {
            alert("i'm the call from inner2")
        },

        inner3: {

            innerCall: function () {
                alert("i'm the call from inner3")
            },

            inner4: {

                innerCall: function () {
                    alert("i'm the call from inner4")
                },

                inner5: {

                    innerCall: function () {
                        alert("i'm the call from inner1")
                    },

                }
            }
        }
    }
}
};
outerMost.inner1.inner2.inner3.inner4.innerCall();

Comments

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.