12

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?

Curt
  • 94,964
  • 60
  • 257
  • 340
Sergey Metlov
  • 23,436
  • 26
  • 87
  • 142
  • 5
    it's a standard in Java, php, Javascript and about any other language – Tom Jul 26 '11 at 12:07
  • If you're going to look at the jQuery code, look at the development copy and not the minified one. **[Here's a link](http://code.jquery.com/jquery-1.6.2.js)** to the development copy. – qwertymk Jul 26 '11 at 12:28
  • 1
    @Tom: the [comma operator](http://ecma262-5.com/ELS5_HTML.htm#Section_11.14) is different in JavaScript that in other languages with a C-inspired syntax. – dolmen Jul 26 '11 at 12:38
  • 4
    Wow, those code minifiers are aggressive - it's only one character shorter than `if(c==null){c=a;a=null}` – Random832 Jul 26 '11 at 13:33

3 Answers3

15

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; }
dascandy
  • 6,916
  • 1
  • 25
  • 49
  • 2
    The comma makes both commands execute without need of a semicolon. – Digital Plane Jul 26 '11 at 12:08
  • 2
    which in turn allows *both* statements to execute in a **single line**, which is a character savings over an if-statement. – zzzzBov Jul 26 '11 at 13:19
  • 2
    @Gunslinger_ : how much faster actually? – Mchl Jul 26 '11 at 15:07
  • Python also allows you to use `A or B` as an equivalent to something like `A != null && A == true ? A : B`. Though in Python you'd say that as `A if A != None and A == True else B`. I might be missing some of the subtleties of Python's usage, though. – JAB Jul 26 '11 at 16:23
  • @JAB that is possible in javascript –  Jul 26 '11 at 21:35
12

That's an ugly way to write

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

This utilizes the fact, that the second part of boolean && will be executed if, and only if the first part evaluates to true.

Mchl
  • 58,281
  • 9
  • 105
  • 116
  • 1
    Very ugly. And very unintuitive to many developers. Can you find the person who wrote it and kick them. – Schroedingers Cat Jul 26 '11 at 12:09
  • 10
    @Schroedingers Cat: as variables are named `a`, `b`, `c` this code has probably been generated by a code minifier. [jQuery currently seems](https://github.com/jquery/jquery/blob/master/Makefile#L8) to use [UglifyJS](https://github.com/mishoo/UglifyJS). – dolmen Jul 26 '11 at 12:48
  • 1
    Uglify? That explains a lot, doesn't it? ;D – Mchl Jul 26 '11 at 15:06
2

The expression uses two JavaScript features :

As a result the expression is equivalent to:

if (c == null) {
    c = a
    a = null
}
dolmen
  • 6,442
  • 5
  • 30
  • 34