1

It is possible to declare 2 more functions in main function like this ?

var jquery4u = {
    init: function() {
        jquery4u.countdown.show();
    },
    countdown: function() {
        show: function() {
            console.log('show');
        },
        hide: function() {
            console.log('hide');
        }
    }
}
jquery4u.init();

and i receive the following error: Uncaught SyntaxError: Unexpected token ( on this line "show: function() {"

2
  • What happens when you try it? Did it work? If not, why not? (It won't work because of invalid syntax.. a function is not an object literal and so those 'keys' - show, hide - are really labels.) Commented Jul 16, 2015 at 23:28
  • show and hide need to be inside of an object {} not a function. Commented Jul 16, 2015 at 23:31

2 Answers 2

2

Remove the function from the right of the countdown (demo)

var jquery4u = {
    init: function() {
        jquery4u.countdown.show();
    },
    countdown: {
        show: function() {
            console.log('show');
        },
        hide: function() {
            console.log('hide');
        }
    }
}
jquery4u.init();

Next time, use jsFiddle to make a demo and click the "JSHint" button.

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

1 Comment

ohhh thank you @Mottie !! also it's a new feature for me that "jshint".. haha thanks!
1

Actually, none of this will work. Unless you make countdown an object or you treat its sub-functions as proper functions. Why: Under countdown, you created an instance of object not a function.

var jquery4u = {
    countdown: function() {
        show = function() {
            console.log('show');
        }
        hide = function() {
            console.log('hide');
        }

        jquery4u.countdown.show();
    }
}

The above code is a valid code so it is possible. Unfortunately it will not return anything.

The proper way to do this is in this format:

var jquery4u = {
    countdown: {
        show: function() {
            console.log('show');
        },
        hide: function() {
            console.log('hide');
        }
    }
}

This will work. You can try it out by calling:

jquery4u.countdown.show();

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.