-1

there is a if statement

if ((sliding || "Window" == type) &&
    nbCasement % 2 != 1 || sliding || "Garage" == type || f({
    name: "hinge"
}),
    nbCasement > 0 && !sliding && f({
    name: "side"

}),
    d && apiProperty)

with && || and ,

I thought it's same with this code:

f({
    name: "hinge"
});
f({
    name: "side"
});
if ((sliding || "Window" == type) &&
    nbCasement % 2 != 1 || sliding || "Garage" == type ||
    nbCasement > 0 && !sliding &&
    d && apiProperty)

but the result is different what I thought.

How is this statement working? which condition make f({name:xxx}) run?

the f defind is

  var e = []
          , f = function(a) {
            d && d.blackList && d.blackList[a.name] || e.push(a)
        };
chanjianyi
  • 607
  • 4
  • 15
  • 35
  • Some context might help us you know... – RononDex Jan 27 '16 at 09:29
  • It would also help if you explained why you thought those code snippets are equivalent so that it would be easier to tell what part of your thought process was wrong. For example, why do you think that both `f()` calls would always run (and their return values would be discarded)? – JJJ Jan 27 '16 at 09:31
  • Where did you find this code? In some minified script? That would explain the formatting. – Bergi Jan 27 '16 at 09:32
  • @Bergi yess. from minified script. hard to understand – chanjianyi Jan 27 '16 at 09:34
  • @Juhana because from here http://stackoverflow.com/a/5348007/1204882 . I think the `f()` always run. – chanjianyi Jan 27 '16 at 09:36
  • @chanjianyi: Try to get the original, instead of trying to understand minifier trickery :-) – Bergi Jan 27 '16 at 09:39
  • But the `f()` calls aren't alone in the expressions. The comma operator doesn't negate the `&&` and `||` operators there. – JJJ Jan 27 '16 at 09:44

1 Answers1

1

The comma operator just evaluates everything, before returning the last value. Your snippet is equivalent to

(sliding || "Window" == type) && nbCasement % 2 != 1 || sliding || "Garage" == type || f({
    name: "hinge"
});
nbCasement > 0 && !sliding && f({
    name: "side"
});
if (d && apiProperty)

which can be cleaned up and prettified to

if (!sliding) {
    if (("Window" != type || nbCasement % 2 == 1) && "Garage" != type)
        f({
            name: "hinge"
        });
    if (nbCasement > 0)
        f({
            name: "side"
        });
}
if (d && apiProperty)
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thankyou! but forget the `sliding` . why `nbCasement > 0 && !sliding && f({ name: "side" });` == `if (nbCasement > 0) {f({ name: "side" });}` – chanjianyi Jan 27 '16 at 09:56
  • See [here](http://stackoverflow.com/q/6970346/1048572) and notice that this part became `if (!sliding) { if (nbCasement > 0) f({…}); }` – Bergi Jan 27 '16 at 09:58