How do you read comma sentences in JavaScript?

9

Today I was testing minify my code and I was in doubt about the conversion.

Original Code

Loader = (function(Configure){

    var CurrentAction = null;
    var loaded = [];

    loader = function(){}

    loader.prototype = {
        check : function(action){
            var _return = false;
            if(CurrentAction == null || Configure.get('reload')){
                loaded.push(action);
                _return = true;
            }

            if(CurrentAction != action && loaded.indexOf(action) == -1){
                loaded.push(action);
                _return = true;
            }
            CurrentAction = action;
            return _return;
        }
    }

    return new loader();
}(Configure));

Minimized Code

Loader = function(e) {
    var n = null,
        r = [];
    return loader = function() {}, loader.prototype = {
        check: function(o) {
            var u = !1;
            return (null == n || e.get("reload")) && (r.push(o), u = !0), n != o && -1 == r.indexOf(o) && (r.push(o), u = !0), n = o, u
        }
    }, new loader
}(Configure);

Doubt

  • How do you read this code with commas?

Addendum

I understand that it does not execute (r.push(o), u = !0) if -1 == r.indexOf(o) has already generated false , but I do not understand when it executes what comes after the comma, n = o, u

    
asked by anonymous 20.07.2016 / 16:34

2 answers

4

This is a precedência de operador technique, and the name of that operator is operador de vírgula (comma operator) or also known as multiple evaluation operator.

It works as follows, it evaluates or calculates all past expressions separated by a comma from left to right and returns the last expression passed.

Here's an example:

var expr1 = 10 + 10,//Cálcula
    expr2 = 100 * 10,//Cálcula
    expr3 = 10 == "10",//Avalia
    expr4 = expr1 === expr2 && expr3;//Essa expressão expr4 que será retornada.

expr1, expr2, expr3, expr4;

This operator is most often used when you want to use more than one expression in a place where you would normally use only one.

Normally this operator is most commonly used in loops for , for example:

var vetor = [
  [1, 2, 3, 4], [5, 6, 7, 8]
];

for (var i = 0, j = 2; i < vetor.length; i++, j--) {
   console.log(vetor[i][j]);
}

Now regarding your mined code, what is returned after the comma is a Boolean value, which is this u variable, which in your original code represents your _return variable.

var u = !1;

Same as:

var _return = false;

E:

u = !0

Same as:

_return = true;

Note: This expression !1 is equal to false and !0 equal true .

Analyzing the conversion (Guilherme Lautert)

(null == n || e.get("reload")) && (r.push(o), u = !0)

  • (r.push(o), u = !0) this part of the code will always return true , however it only executes if the previous sentence tbm is true
  • (null == n || e.get("reload")) is a real check, which if it is false ignores the next sentence because it is a &&

n != o && -1 == r.indexOf(o) && (r.push(o), u = !0)

  • Similar to previous

n = o

  • This is the part that is always executed CurrentAction = action;

u

  • As commented the last part is returned, this would be _return
20.07.2016 / 21:17
2

What is there in front of return is an expression string, and the last expression (where the u identifier was declared) will be returned to the return statement itself. An expression sequence goes to blocks, groups ( (...) ) and other places as well, not just there.

In an expression sequence statements are not declared, only expressions; Already, if you declare the statement var ( let or const ), you do not start an expression string, even if you use commas, however var only goes in command blocks (as I said, statements are not declared in sequence of expression) and in other places, for example, in the arguments of for that are separated by semicolons ( ; ).

And another, the keys declared after the functions and after the statements for , while , with , etc, they start a block, which can have a new scope of variables depending on the type. If these braces are not declared, you can only declare an expression string, and you can also declare the statement var . Example: if(true) var a, b;

Note : try , catch and finally are statements that require the braces to be declared because the browser interpreter currently requires. Mostly function...(...) .

    
20.07.2016 / 17:30