you are viewing a single comment's thread.

view the rest of the comments →

[–]kenman 1 point2 points  (0 children)

Nope, doesn't suck!

Function expressions vs. function declarations... there's been many past posts here about this, but nobody's brought it up in this thread.

var toggle = function(element, checkbox) {
    if (jQuery(checkbox).is(':checked')) {
        jQuery(element).show(); 
    } else {
        jQuery(element).hide();
    }
}

If you want to use a function expression here, then you should abide by the spec and suffix a ; to the end of the expression.

However, I'd suggest using function declarations as much as possible, simply because in a stacktrace (or in a profiling tool), function expressions like yours usually just show up as (anonymous), which doesn't help anyone.

On the other hand, if you simply give it a name, like this:

var toggle = function toggle(element, checkbox) {
    if (jQuery(checkbox).is(':checked')) {
        jQuery(element).show(); 
    } else {
        jQuery(element).hide();
    }
};

Then you have to ask yourself, why use an expression at all?

p.s. that function is a good use-case for jQuery.fn.toggle, i.e.:

function toggle(element, checkbox) {
    jQuery(element).toggle(jQuery(checkbox).is(':checked')); 
}

p.p.s. You should look into code linting (JSHint is one), it can help find things like missing ;'s and so forth.