javascriptoperators

What a strange syntax?


I've found unknown for me code construction on JQuery site. After some formatting it looks like:

function (a,c) {
    c==null && (c=a,a=null);
    return arguments.length>0
        ? this.bind(b,a,c) 
        : this.trigger(b)
}

What does the first line of the function mean? Is it any trick or standard JS code construction?


Solution

  • It's a trick that uses boolean short-circuit evaluation to only do the second half if the first evaluates to true. Perl has this commonly:

    <something> or die
    

    where if the first statement failed, the program ends.

    Read it as

    if (c == null) { c = a; a = null; }