0

Below is some typical jquery that I'm working with. As you will see, variables are declared at a global scope (excuse the terminology), however, I wondered if it was also possible to declare a jquery objects the same way that cn, declares class names.

For instance: var jq = { $html : $('html')};

I haven't managed to get any type of syntax to work as yet.

Thanks in advance.

var Nav = function () {

    var pub = {},
    cn = {
        open: "mobile-nav-open"
    };

    function toggleNav(e) {

        e.preventDefault();

        $html = $('html');

        if ($html.hasClass(cn.open)) {
            $html.removeClass(cn.open);
        } else {
            $html.addClass(cn.open);
        }
    };

    function setupBindings() {
        $(document).on("click", "#navicon", toggleNav);
    };

    pub.init = function () {
        setupBindings();
    };

    return pub;
} ();
1
  • as you've replaced the = with a : , your question has no sense anymore Commented Jan 16, 2015 at 13:47

2 Answers 2

2

jQuery objects are just a class of JavaScript object. There's nothing special about them.

The syntax is the same as your existing code.

Use a : where you have a = inside your object literal.

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

Comments

0

Javascript object uses name-value pairs. if you declare any anonymous variable inside an object, that context of that variable would be the object

var hellow = 'hh'
var x = {
    hellow: 'abc'
}

console.log(hellow)
console.log(x.hellow)

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.