Unusual way to use a jquery function

Asked

Viewed 59 times

4

This answer to a question brought something I didn’t know: a return with two pairs of square bracketsreturn[][] in a role, solving a problem in a simple but unknown way to me. The question called for the return of the correct day of the week, with a list of days of the week in a switch:

<script>
var day;
   function dia(){
    switch (new Date().getDay()) {
        case 0: day = "Segunda"; break;
         case 1: day = "Terça"; break;
         case 2: day = "Quarta"; break;
         case 3: day = "Quinta"; break;
         case 4: day = "Sexta";  break;
         case 5: day = "Sabado"; break;
         case 6: day = "Domingo"; break;
     };
document.getElementById('demo').innerHTLM = "hoje é "   + day;
}
</script>

The answer came in jquery, solving the problem with a few lines of code:

function {
return ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"][new Date().getDay()];
}
console.log("Hoje é " + diaDaSemanaHoje());

Would someone please explain to me why and how this works?

  • 1

    Within the Return this to be declared an array of days of the week ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"] and then the position in the array is selected, It’s the same as doing a = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"] and then Return a[new Date().getDay()]

2 answers

5


Good, This is not jQuery, it’s pure Javascript.

["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"][new Date().getDay()]

That’s a literal of a array and access to one of its elements. Let’s do what Jack says, let’s go in parts:

["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"]

That part is the literal that defines the array. It has 7 elements and all are strings. Each is positioned on an index starting at 0, so the last will be 6.

If you wanted to assign this to a variable it could be like this:

var array = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"];

If you want to access the element 0 of this array could be:

array[0]

If I do not know at the time of code creation what is the index number could use a variable that will have its value coming from somewhere:

array[x]

Or you could use a expression , After all, a variable is still an expression:

array[new Date().getDay()]

It is known that getDay() returns a number from 0 to 6, according to the day of the week. That’s exactly what we need as a value for the index of array that we created. The function is applied to an object that has just been created, and this object is today’s date. It is the same principle of the variable replaced by a literal. Both are context-appropriate expressions. See another way:

var data = new Date();
var diaSemana = data.getDay();
console.log(diaSemana);

Since we don’t need variables, we use:

console.log(new Date().getDay());

But think about it so you can create a variable? Variables are value storage locations in memory with a name. If I need to "calculate" a value, but I don’t need to store it anywhere there, why would I need a variable? Without the variable, I can only use the literal, so instead of the name array I can use the value that defines this array, thus:

var diaDaSemana = new Date().getDay();
return ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"][diaDaSemana];

But even this variable diaDaSemana is not necessary, I can use a literal, I can use the direct value:

return ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"][new Date().getDay()];

In the case of that answer this value is used to return in the function. If the variable is just a name for a value, I can use the value directly in place in the name. see if you understand better the way with unnecessary variables:

function diaDaSemanaHoje() {
    var array = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"];
    var hoje = new Date();
    var diaDaSemanaHoje = hoje.getDay();
    var textoDiaDaSemanaHoje = array[diaDaSemanaHoje];
    return textoDiaDaSemanaHoje;
}
console.log("Hoje é " + diaDaSemanaHoje());

I put in the Github for future reference.

Is magic?

There’s nothing magical about it. It’s the normal language, it didn’t need anything special. Inside the language specification what is coherent, linear, symmetrical, is to function like this.

That I remember the only language mainstream that did not allow this was PHP and today allows.

The ideal way to program is to understand why each space, each comma in the code. It’s only when you’ve mastered this that you’ve actually learned to program. Then you can do anything. Before and getting to this point is just repeating cake recipes already made before by other people. Even when the recipe is so short when using a variable.

In everything written in the code you need to know why you are doing it. You need to understand the role of each token of the code. And how it can be replaced according to the rules of language, which, incredible as it may seem, in the basics, vary less than people realize.

Programming is putting together a lot of Lego piece. You have to know all on each piece.

Nice to have asked :)

4

var a=[1,2];
console.log(a[1]);

Equal:

console.log([1,2][1]);

simply the array is set and accessed on the same line!

Browser other questions tagged

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