0

In the form object below, from within the "check" function, how do I call the "show" and "hide" methods of the notification function?

(function (namespace, $, undefined) {
    var form = {
        check : function(){
            form.notification.show(); // Generates an error
        },

        notification : function(){
            this.show = function(){
                ...
            };
            this.hide = function(){
                ...
            };
        }
    };
}(window.namespace = window.namespace || {}, jQuery));

With form.notification.show() I get the following error:

Uncaught TypeError: Cannot read property 'show' of undefined

2 Answers 2

1

Try to define notification outside form and then refer to it:

var notification : { // no function here
        show : function(){...}, // avoid "this"
        hide : function(){...}
};

var form = {
    check : function(){
        notification.show(); // <-- Use here
    },

    notification : notification // and here
};

(I omitted the jQuery protection code for clarity).

The next problem is that you this.show = will assign the function to whatever this is when the function notification() is executed. this isn't notification!

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

6 Comments

Strange - this doesn't appear to have any effect. I'm wondering now if there's a conflict in my "working" code...
Well, it seems that I do have access to the notification function in my original code. If I run console.log(form.notification) from within the check function, it returns the script. Any thoughts?
See my edits. show is put into this and this !== form.notification (most likely).
You're right - this referred to the form object. Making notification an object seems to be the obvious fix.
I've improved my answer again. If it solves your problem now, please accept it.
|
0

You've enclosed it, so you need to return it and that will expose it for you, if you whip the following in chrome console, you'll see you have access to the form object

(function (namespace, $, undefined) {

var form = {

        check : function(){
            form.notification.show(); // Generates an error
        },

        notification : function(){
            this.show = function(){

            };
            this.hide = function(){

            };
        }


};
return{form:form};}(window.namespace = window.namespace || {}, jQuery));

All i've done to your code is added

return{form:form};

After the form object. Hope this helps

EDIT

If you want to only expose certain parts of the form, for example only notifications, you could modify the return like so:

return{form.notification: notification}

3 Comments

Not entirely sure why it was downvoted either. However, this isn't a viable option as nothing may follow the return statement.
Not true copy the above code and paste it into your console. then type in window.namespace and see what it returns.
I downvoted it because at that point the answer was just a block of code with no explanation. Also, it's almost always a bad idea to reference an object by its external name from within that object - in most cases it should be accessible as this.

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.