How is the reading of sentences with comma in Javascript?

Asked

Viewed 215 times

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));

Shortcode

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 to read this code with commas?

Addendum

I even understand he didn’t perform (r.push(o), u = !0) if -1 == r.indexOf(o) have already generated false, but I do not understand when it is executed what comes after the comma, n = o, u

2 answers

4


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

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

Follow 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 commonly used when you want to use more than one expression in a place where one would normally be used.

Usually this operator is more 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 minified code, what is returned after the comma is a boolean value, which is this variable u, which in its original code represents its variable _return.

var u = !1;

Equal to:

var _return = false;

And:

u = !0

Equal to:

_return = true;

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

Analyzing the conversion (William Lautert)

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

  • (r.push(o), u = !0) this part of the code will always resume true, however it is only executed if the previous sentence tbm is true
  • (null == n || e.get("reload")) is a real check, 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 the previous

n = o

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

u

  • As commented the last part is returned, this would be the _return
  • 4

    Just an addendum so no one gets confused: the comma in var n = null, r = []; is another thing: it is part of the syntax of var, is not the comma Operator.

  • 2

    I understood, and analyzing again the code became clearer. If you do not mind I would like to add some delete them.

  • Okay, all right, you can add.

2

What’s in front of return is an expression string, and the last expression (where the identifier has been declared u) will be returned to the statement itself return. 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 sequence, even if you use commas, but var only goes in command blocks (as I said, statements are not declared in expression sequence) and elsewhere, for example in the arguments of for which are separated by semicolons (;).


And another, the braces 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, it is possible to declare only an expression string, and it is also possible to declare the statement var. Example: if(true) var a, b;

Observing: try, catch and finally are statements that need braces to be declared because the browser interpreter currently requires. Mainly function...(...).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.